# A Practical Introduction To Python Programming

2y ago
73 Views
6 Downloads
1.95 MB
263 Pages
Last View : 23d ago
Last Download : 4m ago
Upload by : Milena Petrie
Transcription

A Practical Introduction toPython ProgrammingBrian HeinoldDepartment of Mathematics and Computer ScienceMount St. Mary’s University

ii 2012 Brian HeinoldLicensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

ContentsIBasics11Getting Started1.1 Installing Python1.2 IDLE . . . . . . . .1.3 A first program .1.4 Typing things in .1.5 Getting input . . .1.6 Printing . . . . . .1.7 Variables . . . . .1.8 Exercises . . . . .234.333456679For loops2.1 Examples . . . . . . .2.2 The loop variable . .2.3 The range function2.4 A Trickier Example .2.5 Exercises . . . . . . .111113131415.Numbers3.1 Integers and Decimal Numbers3.2 Math Operators . . . . . . . . . . .3.3 Order of operations . . . . . . . .3.4 Random numbers . . . . . . . . .3.5 Math functions . . . . . . . . . . .3.6 Getting help from Python . . . .3.7 Using the Shell as a Calculator .3.8 Exercises . . . . . . . . . . . . . . .191919212121222223If statements4.1 A Simple Example . .4.2 Conditional operators4.3 Common Mistakes . .4.4 elif . . . . . . . . . . .4.5 Exercises . . . . . . . .272728282930.iii

iv5678CONTENTSMiscellaneous Topics I5.1 Counting . . . . . .5.2 Summing . . . . . .5.3 Swapping . . . . . .5.4 Flag variables . . .5.5 Maxes and mins . .5.6 Comments . . . . .5.7 Simple debugging5.8 Example programs5.9 Exercises . . . . . .Strings6.1 Basics . . . . . . . . . . . . . . . . . . . . . . . . .6.2 Concatenation and repetition . . . . . . . . .6.3 The in operator . . . . . . . . . . . . . . . . . .6.4 Indexing . . . . . . . . . . . . . . . . . . . . . . .6.5 Slices . . . . . . . . . . . . . . . . . . . . . . . . .6.6 Changing individual characters of a string6.7 Looping . . . . . . . . . . . . . . . . . . . . . . .6.8 String methods . . . . . . . . . . . . . . . . . .6.9 Escape characters . . . . . . . . . . . . . . . . .6.10 Examples . . . . . . . . . . . . . . . . . . . . . .6.11 Exercises . . . . . . . . . . . . . . . . . . . . . .Lists7.1 Basics . . . . . . . . . . .7.2 Similarities to strings7.3 Built-in functions . . .7.4 List methods . . . . . .7.5 Miscellaneous . . . . .7.6 Examples . . . . . . . .7.7 Exercises . . . . . . . .More with Lists8.1 Lists and the random module8.2 split . . . . . . . . . . . . . . . .8.3 join . . . . . . . . . . . . . . . . .8.4 List comprehensions . . . . . . .8.5 Using list comprehensions . . .8.6 Two-dimensional lists . . . . . .8.7 Exercises . . . . . . . . . . . . . 7585959606062.6565666768697072

CONTENTS9vWhile loops9.1 Examples . . . . . . . . . . . . . . . . . . .9.2 Infinite loops . . . . . . . . . . . . . . . . .9.3 The break statement . . . . . . . . . . .9.4 The else statement . . . . . . . . . . . . .9.5 The guessing game, more nicely done9.6 Exercises . . . . . . . . . . . . . . . . . . .10 Miscellaneous Topics II10.1 str, int, float, and list10.2 Booleans . . . . . . . . . . . . . . .10.3 Shortcuts . . . . . . . . . . . . . .10.4 Short-circuiting . . . . . . . . . .10.5 Continuation . . . . . . . . . . . .10.6 pass . . . . . . . . . . . . . . . . .10.7 String formatting . . . . . . . . .10.8 Nested loops . . . . . . . . . . . .10.9 Exercises . . . . . . . . . . . . . .75757878798083.8787899091919192939511 Dictionaries11.1 Basics . . . . . . . . . . . . . .11.2 Dictionary examples . . . .11.3 Working with dictionaries11.4 Counting words . . . . . . .11.5 Exercises . . . . . . . . . . .999910010110210412 Text Files12.1 Reading from files12.2 Writing to files . . .12.3 Examples . . . . . .12.4 Wordplay . . . . . .12.5 Exercises . . . . . .10910911011011111313 Functions13.1 Basics . . . . . . . . . . . . . . . . . . . . . . . . . .13.2 Arguments . . . . . . . . . . . . . . . . . . . . . .13.3 Returning values . . . . . . . . . . . . . . . . . .13.4 Default arguments and keyword arguments13.5 Local variables . . . . . . . . . . . . . . . . . . .13.6 Exercises . . . . . . . . . . . . . . . . . . . . . . .11911912012112212312514 Object-Oriented Programming14.1 Python is objected-oriented14.2 Creating your own classes .14.3 Inheritance . . . . . . . . . . .14.4 A playing-card example . .129129130132133.

viCONTENTS14.5 A Tic-tac-toe example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13614.6 Further topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13814.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138IIGraphics15 GUI Programming with Tkinter15.1 Basics . . . . . . . . . . . . . . .15.2 Labels . . . . . . . . . . . . . .15.3 grid . . . . . . . . . . . . . . .15.4 Entry boxes . . . . . . . . . . .15.5 Buttons . . . . . . . . . . . . .15.6 Global variables . . . . . . . .15.7 Tic-tac-toe . . . . . . . . . . . .141.14314314414514614614814916 GUI Programming II16.1 Frames . . . . . . . . . . . . . . . . . .16.2 Colors . . . . . . . . . . . . . . . . . .16.3 Images . . . . . . . . . . . . . . . . . .16.4 Canvases . . . . . . . . . . . . . . . .16.5 Check buttons and Radio buttons16.6 Text widget . . . . . . . . . . . . . .16.7 Scale widget . . . . . . . . . . . . .16.8 GUI Events . . . . . . . . . . . . . . .16.9 Event examples . . . . . . . . . . . .15515515615715815916016116216417 GUI Programming III17.1 Title bar . . . . . . . . . . . . . .17.2 Disabling things . . . . . . . . .17.3 Getting the state of a widget .17.4 Message boxes . . . . . . . . . .17.5 Destroying things . . . . . . . .17.6 Updating . . . . . . . . . . . . .17.7 Dialogs . . . . . . . . . . . . . .17.8 Menu bars . . . . . . . . . . . .17.9 New windows . . . . . . . . . .17.10 pack . . . . . . . . . . . . . . . .17.11 StringVar . . . . . . . . . . .17.12More with GUIs . . . . . . . . .169169169169170171171172174174175175176.18 Further Graphical Programming17718.1 Python 2 vs Python 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17718.2 The Python Imaging Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17918.3 Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

CONTENTSIIIviiIntermediate Topics18319 Miscellaneous topics III19.1 Mutability and References . . . . . . . . . . . . . . . . . .19.2 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19.3 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19.4 Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19.5 sorted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19.6 if-else operator . . . . . . . . . . . . . . . . . . . . . . .19.7 continue . . . . . . . . . . . . . . . . . . . . . . . . . . . .19.8 eval and exec . . . . . . . . . . . . . . . . . . . . . . . .19.9 enumerate and zip . . . . . . . . . . . . . . . . . . . .19.10 copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19.11More with strings . . . . . . . . . . . . . . . . . . . . . . . .19.12Miscellaneous tips and tricks . . . . . . . . . . . . . . . .19.13Running your Python programs on other 9620 Useful modules20.1 Importing modules . . . . . . . . . .20.2 Dates and times . . . . . . . . . . . . .20.3 Working with files and directories .20.4 Running and quitting programs . .20.5 Zip files . . . . . . . . . . . . . . . . . .20.6 Getting files from the internet . . . .20.7 Sound . . . . . . . . . . . . . . . . . . .20.8 Your own modules . . . . . . . . . . .19919920020220420420520520621 Regular expressions21.1 Introduction . .21.2 Syntax . . . . . .21.3 Summary . . . .21.4 Groups . . . . . .21.5 Other functions21.6 Examples . . . .20720720821221421421622 Math22.1 The math module . . . . . . . . . . .22.2 Scientific notation . . . . . . . . . . .22.3 Comparing floating point numbers22.4 Fractions . . . . . . . . . . . . . . . . .22.5 The decimal module . . . . . . . .22.6 Complex numbers . . . . . . . . . . .22.7 More with lists and arrays . . . . . .22.8 Random numbers . . . . . . . . . . .22.9 Miscellaneous topics . . . . . . . . . .219219220221221222224226226228.

be a concise, but not superﬁcial, treatment on GUI programming. Part III contains information on the features of Python that allow you to accomplish big things with surprisingly little code. In preparing this book the Python documentation atwww.python.orgwas indispensable. This book was composed entirely in LATEX.

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

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

Launch Eclipse Install Python plug-in for Eclipse Add a Python Interpreter Create a Python Project Create a Python Program Run a Python Program Debug a Python Program 0 Introduction This tutorial is for students who want to develop Python projects using Eclipse. E

Introduction to basic Python Contents 1. Installing Python 2. How to run Python code 3. How to write Python code 4. How to troubleshoot Python code 5. Where to go to learn more Python is an astronomer's secret weapon. With Python, the process of visualizing, processing, and interacting with data is made extremely simple.

A Python Book A Python Book: Beginning Python, Advanced Python, and Python Exercises Author: Dave Kuhlman Contact: dkuhlman@davekuhlman.org

Mike Driscoll has been programming with Python for more than a decade. He has been writing about Python on his blog, The Mouse vs. The Python, for many years. Mike is the author of several Python books including Python 101, Python Interviews, and ReportLab: PDF Processing with Python. You can find Mike on Twitter or GitHub via his handle .