Python Programming For

3y ago
69 Views
4 Downloads
854.21 KB
70 Pages
Last View : 10d ago
Last Download : 5m ago
Upload by : Halle Mcleod
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. . . . . . . . . . . . . .

vi Sams Teach Yourself Python Programming for Raspberry Pi in 24 Hours Learning About the Python Interpreter. . . . . . . . . . . . . . . . . . . . . . . . . . .50 .

Related Documents:

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Python is readable 5 Python is complete—"batteries included" 6 Python is cross-platform 6 Python is free 6 1.3 What Python doesn't do as well 7 Python is not the fastest language 7 Python doesn't have the most libraries 8 Python doesn't check variable types at compile time 8 1.4 Why learn Python 3? 8 1.5 Summary 9

Bruksanvisning för bilstereo . Bruksanvisning for bilstereo . Instrukcja obsługi samochodowego odtwarzacza stereo . Operating Instructions for Car Stereo . 610-104 . SV . Bruksanvisning i original

site "Python 2.x is legacy, Python 3.x is the present and future of the language". In addition, "Python 3 eliminates many quirks that can unnecessarily trip up beginning programmers". However, note that Python 2 is currently still rather widely used. Python 2 and 3 are about 90% similar. Hence if you learn Python 3, you will likely

There are currently two versions of Python in use; Python 2 and Python 3. Python 3 is not backward compatible with Python 2. A lot of the imported modules were only available in Python 2 for quite some time, leading to a slow adoption of Python 3. However, this not really an issue anymore. Support for Python 2 will end in 2020.

Python Programming - This is a textbook in Python Programming with lots of Practical Examples and Exercises. You will learn the necessary foundation for basic programming with focus on Python. Python for Science and Engineering - This is a textbook in Python Programming with lots of Examples, Exercises, and Practical Applications

CR ASH COURSE PY THON CR ASH COURSE 2ND EDITION ERIC MATTHES SHELVE IN: PROGRAMMING LANGUAGES/ PYTHON 39.95 ( 53.95 CDN) LEARN PYTHON— FAST! COVERS PYTHON 3.X Python Crash Course is the world's best-selling guide to the Python programming language. This fast-paced, thorough introduction to programming with Python will