Python Programming For

Transcription

Richard BlumChristine BresnahanPythonProgramming forRaspberry Pi

Sams Teach Yourself Python Programming for Raspberry Pi 24 HoursCopyright 2014 by Pearson Education, Inc.All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, ortransmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, withoutwritten permission from the publisher. No patent liability is assumed with respect to the use ofthe information contained herein. Although every precaution has been taken in the preparation ofthis book, the publisher and author assume no responsibility for errors or omissions. Nor is anyliability assumed for damages resulting from the use of the information contained herein.Raspberry Pi is a trademark of the Raspberry Pi Foundation.ISBN-13: 978-0-7897-5205-5ISBN-10: 0-7897-5205-0Library of Congress Control Number: 2013946052Printed in the United States of AmericaFirst Printing: October 2013Editor-in-ChiefGreg WiegandExecutive EditorRick KughenDevelopmentEditorMark RenfrowManaging EditorKristy HartProject EditorAndy BeasterCopy EditorKitty WilsonTrademarksAll terms mentioned in this book that are known to be trademarks or service marks have beenappropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Useof a term in this book should not be regarded as affecting the validity of any trademark or servicemark.Warning and DisclaimerEvery effort has been made to make this book as complete and as accurate as possible, but nowarranty or fitness is implied. The information provided is on an “as is” basis. The author(s) andthe publisher shall have neither liability nor responsibility to any person or entity with respect toany loss or damages arising from the information contained in this book.Bulk SalesSams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contactU.S. Corporate and Government or sales outside of the U.S., please contactInternational Salesinternational@pearsoned.comIndexerTim WrightProofreaderSarah KearnsTechnical EditorJason FosterPublishingCoordinatorKristen WattersonInterior DesignerMark ShirarCover DesignerMark ShirarCompositorNonie Ratcliff

Contents at a GlanceIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Part I: The Raspberry Pi Programming EnvironmentHOUR 1 Setting Up the Raspberry Pi.72 Understanding the Raspbian Linux Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Setting Up a Programming Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Part II: Python FundamentalsHOUR 4 Understanding Python Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715 Using Arithmetic in Your Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976 Controlling Your Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1157 Learning About Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131Part III: Advanced PythonHOUR 8 Using Lists and Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1559 Dictionaries and Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17510 Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19911 Using Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21712 Creating Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23913 Working with Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25714 Exploring the World of Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . 27715 Employing Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29316 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31717 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

ivSams Teach Yourself Python Programming for Raspberry Pi in 24 HoursPart IV: Graphical ProgrammingHOUR 18 GUI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36119 Game Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387Part V: Business ProgrammingHOUR 20 Using the Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42321 Using Databases in Your Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44722 Web Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469Part VI: Raspberry Pi Python ProjectsHOUR 23 Creating Basic Pi/Python Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49324 Working with Advanced Pi/Python Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529Part VII: AppendixA Loading the Raspbian Operating System onto an SD Card . . . . . . . . . . . . . . 553Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561

Table of ContentsIntroduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Programming with Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Conventions Used in This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1123Part I: The Raspberry Pi Programming EnvironmentHOUR 1: Setting Up the Raspberry Pi.What Is a Raspberry Pi?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7Acquiring a Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Nice Additional Peripherals .9. . . . . . . . . . . . . . . . . . . . .11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17What Raspberry Pi Peripherals Are Necessary?. . . . . . . . . . . . . . . . . . . . . . . . . . .19Getting Your Raspberry Pi Working. . . . . . . . . . . . . . . . . . . . . . . . . . . . .19Troubleshooting Your Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25Deciding How to Purchase PeripheralsSummary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 2: Understanding the Raspbian Linux Distribution .Learning About Linux . . . . . . . . . . . . . . . . .27. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27. . . . . . . . . . . . . . . . . . . . .28. . . . . . . . . . . . . . . . . . . . . . . . . . . . .33. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43Interacting with the Raspbian Command LineInteracting with the Raspbian GUI .Summary .Q&A26Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 3: Setting Up a Programming EnvironmentExploring Python44. . . . . . . . . . . . . . . . . . . . . .45. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45. . . . . . . . . . . . . . . . . . . . . . . . . . . .46. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48Checking Your Python Environment .Installing Python and Tools .

viSams Teach Yourself Python Programming for Raspberry Pi in 24 HoursLearning About the Python Interpreter . . . . . . . . . . . . . . . . . . . . . . . . . .Learning About the Python Interactive Shell . . . . . . . . . . . . . . . . . . . . . .54. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59. . . . . . . . . . . . . . . . . . . . . . . . . .65. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66Knowing Which Tool to Use and When.Summary .Q&A51. . . . . . . . . . .Learning About the Python Development Environment ShellCreating Python Scripts .50Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67Part II: Python FundamentalsHOUR 4: Understanding Python BasicsProducing Python Script Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81Formatting Scripts for Readability.Understanding Python Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . .82. . . . . . . . . . . . . . . . . . . . . . . . . . . . .86. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95Assigning Value to Python VariablesLearning About Python Data TypesAllowing Python Script Input .Summary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 5: Using Arithmetic in Your ProgramsWorking with Math Operators .Calculating with Fractions . . . . . . . . . . . . . . . . . . . . . . . . . .97. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using Complex Number Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Getting Fancy with the math Module .103105. . . . . . . . . . . . . . . . . . . . . . . . . . .106. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113Using the NumPy Math LibrariesSummary .Q&A95Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 6: Controlling Your Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Working with the if StatementGrouping Multiple Statements113115. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117

ContentsviiAdding Other Options with the else Statement . . . . . . . . . . . . . . . . . . .118Adding More Options Using the elif Statement. . . . . . . . . . . . . . . . . . .120. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121Comparing Values in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129Checking Complex Conditions .Negating a Condition CheckSummary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 7: Learning About Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Performing Repetitive Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the for Loop for Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the while Loop for Iteration131131132. . . . . . . . . . . . . . . . . . . . . . . . . . . . .143. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150Creating Nested Loops .Summary .Q&A129Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151Part III: Advanced PythonHOUR 8: Using Lists and Tuples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160Introducing TuplesIntroducing Lists .Using Multidimensional Lists to Store Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168. . . . . . . . . . . . . . . . . . . . .170. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172Working with Lists and Tuples in Your Scripts .Creating Lists by Using List Comprehensions .Working with RangesSummary .Q&A167Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 9: Dictionaries and Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Understanding Python Dictionary Terms .Exploring Dictionary Basics .172175. . . . . . . . . . . . . . . . . . . . . . . .175. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176Programming with Dictionaries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182

viiiSams Teach Yourself Python Programming for Raspberry Pi in 24 Hours. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187Understanding Python SetsExploring Set Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . .189. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192Obtaining Information from a SetModifying a Set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .193. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197Programming with SetsSummary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197HOUR 10: Working with Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199The Basics of Using Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199. . . . . . . . . . . . . . . . . . . . . . . . . .202. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215Using Functions to Manipulate StringsFormatting Strings for OutputSummary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 11: Using Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Understanding Linux File Structures .215217. . . . . . . . . . . . . . . . . . . . . . . . . . .217Opening a File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221Reading a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230Closing a File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237Writing to a FileSummary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 12: Creating Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Utilizing Python Functions in Your Programs.Returning a Value .238239. . . . . . . . . . . . . . . . . . . . .239. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243Passing Values to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253Handling Variables in a Function .Using Lists with Functions244Using Recursion with Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .254

Contents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .256Summary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 13: Working with Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Introducing Module ConceptsExploring Standard Modules257. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261265. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275Summary .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 14: Exploring the World of Object-Oriented Programming .Defining Class Methods .276. . . . . . . . . .277. . . . . . . . . . .277. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .280Understanding the Basics of Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . . . .287. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .290. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .290Sharing Your Code with Class Modules .Summary .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 15: Employing Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Learning About the Class Problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using Inheritance in Python290293293. . . . . . . . . . . . . . . . . . . . . . .294. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296Understanding Subclasses and Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . .302. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314Using Inheritance in Python ScriptsSummary .Q&A257. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Custom ModulesQ&A256. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Learning About Python ModulesQ&AixWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 16: Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .What Are Regular Expressions?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .317317. . . . . . . . . . . . . . . . . . . . .319. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .321Working with Regular Expressions in Python.Defining Basic Patterns315Using Advanced Regular Expressions Features . . . . . . . . . . . . . . . . . . . .330

xSams Teach Yourself Python Programming for Raspberry Pi in 24 Hours. . . . . . . . . . . .332. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .334. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335Working with Regular Expressions in Your Python ScriptsSummary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335HOUR 17: Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337Understanding Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .342Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .344. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .357. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .357Handling Multiple ExceptionsSummary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .357Part IV: Graphical ProgrammingHOUR 18: GUI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Programming for a GUI Environment . . . . . . . . . . . . . . . . . . . . . . . . . .361. . . . . . . . . . . . . . . . . . . . . . . . . . . . .363. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364Examining Python GUI Packages .Using the tkinter PackageExploring the tkinter Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .372. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .384. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .384Summary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 19: Game Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Understanding Game Programming . . . . . . . . . . . . . . . . . . . . . . . . . . .387387. . . . . . . . . . . . . . . . . .388389. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .394Setting Up the PyGame Library.Using PyGame384. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Learning About Game Frameworks and LibrariesLearning More About PyGame .Dealing with PyGame Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .403. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .408. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .418. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .418Summary .Q&A361Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .418

ContentsxiPart V: Business ProgrammingHOUR 20: Using the Network. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Finding the Python Network Modules . . . . . . . . . . . . . . . . . . . . . . . . . .423. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .424. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .432Working with Email Servers .Working with Web Servers . . . . . . . . . . . . . . . . . . . .438. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .445. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .445Linking Programs Using Socket Programming .Summary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 21: Using Databases in Your ProgrammingWorking with the MySQL Database446. . . . . . . . . . . . . . . . . . . . .447. . . . . . . . . . . . . . . . . . . . . . . . . . . .447. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .458. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .466. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .466Using the PostgreSQL DatabaseSummary .Q&A423Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 22: Web Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .467469Running a Web Server on the Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469Programming with the CommonGateway Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .473. . . . . . . . . . . . . . . . . . . . . . . . . . . .475. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .482. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .488. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .488Expanding Your Python Webpages .Processing Forms .Summary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .488Part VI: Raspberry Pi Python Projects1HOUR 23: Creating Basic Pi/Python Projects . . . . . . . . . . . . . . . . . . . . . . . .493Thinking About Basic Pi/Python Projects . . . . . . . . . . . . . . . . . . . . . . . .493. . . . . . . . . . . . . . . . . . . . . . . . . . . . .493. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .514Displaying HD Images via PythonPlaying Music .Creating a Special Presentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .521

xiiSams Teach Yourself Python Programming for Raspberry Pi in 24 Hours. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .526. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .526Summary .Q&AWorkshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 24: Working with Advanced Pi/Python Projects. . . . . . . . . . . . . . . . . .529. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .529. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .535Exploring the GPIO Interface .Using the RPi.GPIO Module. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .536. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .542. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .548. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .549Controlling GPIO Output .Detecting GPIO InputSummary .Q&A526Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .549Part VII: AppendixAPPENDIX A: Loading the Raspbian Operating System onto an SD Card .Windows: Loading Raspbian onto an SD Card553. . . . . . . . . . . . . . . . . . . .553. . . . . . . . . . . . . . . . . . . . . .556. . . . . . . . . . . . . . . . . . . . . . . .558. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .561Linux: Loading Raspbian onto an SD Card .Mac: Loading Raspbian onto an SD CardIndex . . . .

About the AuthorsRichard Blum has worked in the IT industry for over 25 years as a network and systemsadministrator, managing Microsoft, Unix, Linux, and Novell servers for a network withmore than 3,500 users. He has developed and teaches programming and Linux coursesvia the Internet to colleges and universities worldwide. Rich has a master’s degree in management information systems from Purdue University and is the author of several Linuxbooks, including Linux Command Line and Shell Scripting Bible (coauthored with ChristineBresnahan, 2011, Wiley), Linux for Dummies, 9th edition (2009, Wiley), and Professional LinuxProgramming (coauthored with Jon Masters, 2007, Wiley). When he’s not busy being a computer nerd, Rich enjoys spending time with his wife, Barbara, and two daughters, Katie Janeand Jessica.Christine Bresnahan started working in the IT industry more than 25 years ago as asystem administrator. Christine is currently an adjunct professor at Ivy Tech CommunityCollege in Indianapolis, Indiana, teaching Python programming, Linux system administration, and Linux security classes. Christine produces Unix/Linux educational material and isthe author of Linux Bible, 8th edition (coauthored with Christopher Negus, 2012, Wiley) andLinux Command Line and

Bresnahan, 2011, Wiley), Linux for Dummies , 9th edition (2009, Wiley), and Professional Linux Programming (coauthored with J