Python Programming
Total Page:16
File Type:pdf, Size:1020Kb
Python Programming en.wikibooks.org December 27, 2015 On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. A URI to this license is given in the list of figures on page 187. If this document is a derived work from the contents of one of these projects and the content was still licensed by the project under this license at the time of derivation this document has to be licensed under the same, a similar or a compatible license, as stated in section 4b of the license. The list of contributors is included in chapter Contributors on page 179. The licenses GPL, LGPL and GFDL are included in chapter Licenses on page 191, since this book and/or parts of it may or may not be licensed under one or more of these licenses, and thus require inclusion of these licenses. The licenses of the figures are given in the list of figures on page 187. This PDF was generated by the LATEX typesetting software. The LATEX source code is included as an attachment (source.7z.txt) in this PDF file. To extract the source from the PDF file, you can use the pdfdetach tool including in the poppler suite, or the http://www. pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility. Some PDF viewers may also let you save the attachment to a file. After extracting it from the PDF file you have to rename it to source.7z. To uncompress the resulting archive we recommend the use of http://www.7-zip.org/. The LATEX source itself was generated by a program written by Dirk Hünniger, which is freely available under an open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf. Contents 1 Overview 3 2 Getting Python 5 2.1 Python 2 vs Python 3 ............................. 5 2.2 Installing Python in Windows ......................... 5 2.3 Installing Python on Mac ........................... 6 2.4 Installing Python on Unix environments ................... 6 2.5 Keeping Up to Date .............................. 8 2.6 Notes ....................................... 9 3 Interactive mode 11 4 Creating Python programs 13 4.1 Hello, World! .................................. 13 4.2 Exercises ..................................... 16 4.3 Notes ....................................... 16 5 Basic syntax 17 6 Data types 21 6.1 Null object .................................... 25 6.2 Exercises ..................................... 25 7 Numbers 27 8 Strings 29 8.1 String operations ................................ 29 8.2 String constants ................................. 31 8.3 String methods ................................. 31 8.4 Exercises ..................................... 37 8.5 External links .................................. 37 9 Lists 39 9.1 Overview ..................................... 39 9.2 List creation ................................... 39 9.3 List Attributes ................................. 42 9.4 Combining lists ................................. 42 9.5 Getting pieces of lists (slices) ......................... 43 9.6 Comparing lists ................................. 44 9.7 Sorting lists ................................... 44 9.8 Iteration ..................................... 45 III Contents 9.9 Removing .................................... 46 9.10 Aggregates .................................... 46 9.11 Copying ..................................... 46 9.12 Clearing ..................................... 47 9.13 List methods .................................. 48 9.14 operators ..................................... 48 9.15 Subclassing ................................... 49 9.16 Exercises ..................................... 49 9.17 External links .................................. 49 10 Dictionaries 51 10.1 Overview ..................................... 51 10.2 Dictionary notation ............................... 51 10.3 Operations on Dictionaries ........................... 52 10.4 Combining two Dictionaries .......................... 52 10.5 Deleting from dictionary ............................ 52 10.6 Exercises ..................................... 52 10.7 External links .................................. 53 11 Sets 55 12 Operators 61 12.1 Basics ...................................... 61 12.2 Powers ...................................... 61 12.3 Division and Type Conversion ......................... 61 12.4 Modulo ...................................... 62 12.5 Negation ..................................... 62 12.6 Comparison ................................... 62 12.7 Identity ..................................... 63 12.8 Augmented Assignment ............................ 63 12.9 Boolean ..................................... 64 12.10 Exercises ..................................... 64 12.11 References .................................... 64 13 Flow control 65 13.1 Exercises ..................................... 70 13.2 External links .................................. 71 14 Functions 73 14.1 Function Calls .................................. 73 14.2 Closures ..................................... 77 14.3 Lambda Expressions .............................. 78 15 Scoping 81 16 Exceptions 83 17 Input and output 87 17.1 Input ....................................... 87 IV Contents 17.2 Output ...................................... 90 17.3 External Links ................................. 93 18 Modules 95 18.1 Importing a Module .............................. 95 18.2 Creating a Module ............................... 96 18.3 External links .................................. 97 19 Classes 99 20 Metaclasses 125 21 Reflection 129 21.1 Type ....................................... 129 21.2 Isinstance .................................... 129 21.3 Duck typing ................................... 129 21.4 Callable ..................................... 130 21.5 Dir ........................................ 130 21.6 Getattr ...................................... 130 21.7 External links .................................. 130 22 Regular Expression 131 22.1 Overview ..................................... 131 22.2 Matching and searching ............................ 131 22.3 Replacing .................................... 133 22.4 Splitting ..................................... 134 22.5 Escaping ..................................... 134 22.6 Flags ....................................... 134 22.7 Pattern objects ................................. 135 22.8 External links .................................. 135 23 GUI Programming 137 23.1 Tkinter ...................................... 137 23.2 PyGTK ..................................... 138 23.3 PyQt ....................................... 138 23.4 wxPython .................................... 139 23.5 Dabo ....................................... 139 23.6 pyFltk ...................................... 140 23.7 Other Toolkits .................................. 140 24 Authors 141 24.1 Authors of Python textbook . 141 25 Game Programming in Python 143 25.1 3D Game Programming ............................ 143 25.2 2D Game Programming ............................ 144 25.3 See Also ..................................... 145 V Contents 26 Sockets 147 26.1 HTTP Client .................................. 147 26.2 NTP/Sockets .................................. 147 27 Files 149 27.1 File I/O ..................................... 149 27.2 Testing Files ................................... 150 27.3 Common File Operations ............................ 151 27.4 Finding Files .................................. 151 27.5 Current Directory ................................ 152 27.6 External Links ................................. 152 28 Database Programming 153 28.1 Generic Database Connectivity using ODBC . 153 28.2 Postgres connection in Python . 154 28.3 MySQL connection in Python . 154 28.4 SQLAlchemy in Action ............................. 154 28.5 See also ..................................... 154 28.6 References .................................... 155 28.7 External links .................................. 155 29 Web Page Harvesting 157 30 Threading 159 30.1 Examples .................................... 159 31 Extending with C 161 31.1 Using the Python/C API ............................ 161 31.2 Using SWIG ................................... 164 32 Extending with C++ 167 32.1 A Hello World Example ............................ 167 32.2 An example with CGAL ............................ 168 32.3 Handling Python objects and errors . 170 33 Extending with ctypes 171 33.1 Basics ...................................... 171 33.2 Getting Return Values ............................. 171 34 WSGI web programming 173 35 WSGI Web Programming 175 35.1 External Resources ............................... 175 36 References 177 36.1 Language reference ............................... 177 37 Contributors 179 List of Figures 187 VI Contents 38 Licenses 191 38.1 GNU GENERAL PUBLIC LICENSE . 191 38.2 GNU Free Documentation License . 192 38.3 GNU Lesser General Public License . 193 1 1 Overview Python1 is a high-level2, structured3, open-source4 programming language that can be used for a wide variety of programming tasks. Python was created by Guido Van Rossum in the early 1990s, its following has grown steadily and interest is increased markedly in the last few years or so. It is named after Monty Python’s Flying Circus comedy program. Python5 is used extensively for system administration (many vital components of Linux6 Distributions are written in it), also its a great language to teach programming to novice. NASA has used Python for its software