Python Programming
Total Page:16
File Type:pdf, Size:1020Kb
Python Programming Wikibooks.org June 22, 2012 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. An URI to this license is given in the list of figures on page 149. 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 143. The licenses GPL, LGPL and GFDL are included in chapter Licenses on page 153, 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 149. 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, we recommend the use of http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility or clicking the paper clip attachment symbol on the lower left of your PDF Viewer, selecting Save Attachment. 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. This distribution also contains a configured version of the pdflatex compiler with all necessary packages and fonts needed to compile the LATEX source included in this PDF file. 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 3 Interactive mode 9 4 Creating Python programs 11 4.1 Hello, World! . 11 4.2 Exercises . 13 4.3 Notes . 13 5 Basic syntax 15 6 Data types 19 7 Numbers 21 8 Strings 23 8.1 String manipulation . 23 9 Lists 33 9.1 About lists in Python . 33 9.2 List methods . 38 9.3 operators . 39 10 Dictionaries 41 10.1 About dictionaries in Python . 41 11 Sets 43 12 Operators 49 12.1 Basics . 49 12.2 Powers . 49 12.3 Division and Type Conversion . 49 12.4 Modulo . 50 12.5 Negation . 50 12.6 Augmented Assignment . 50 III Contents 12.7 Boolean . 51 12.8 References . 51 13 Flow control 53 14 Functions 59 15 Scoping 63 16 Exceptions 65 17 Input and output 69 17.1 Input . 69 17.2 Output . 72 18 Modules 75 18.1 Importing a Module . 75 18.2 Creating a Module . 76 18.3 External links . 77 19 Classes 79 20 MetaClasses 95 21 Regular Expression 99 21.1 Pattern objects . 99 21.2 Matching and searching . 100 21.3 Replacing . 102 21.4 Other functions . 102 21.5 External links . 103 22 GUI Programming 105 22.1 Tkinter . 105 22.2 PyGTK . 106 22.3 PyQt . 106 22.4 wxPython . 106 22.5 Dabo . 107 22.6 pyFltk . 108 22.7 Other Toolkits . 108 23 Game Programming in Python 109 23.1 3D Game Programming . 109 23.2 2D Game Programming . 110 23.3 See Also . 111 24 Sockets 113 24.1 HTTP Client . 113 24.2 NTP/Sockets . 113 IV Contents 25 Files 115 25.1 File I/O . 115 25.2 Testing Files . 116 25.3 Common File Operations . 117 26 Database Programming 119 26.1 Generic Database Connectivity using ODBC . 119 26.2 Postgres connection in Python . 120 26.3 MySQL connection in Python . 120 26.4 SQLAlchemy in Action . 120 26.5 See also . 120 26.6 References . 120 26.7 External links . 120 27 Web Page Harvesting 121 28 Threading 123 28.1 Examples . 123 29 Extending with C 125 29.1 Using the Python/C API . 125 29.2 Using SWIG . 128 30 Extending with C++ 131 30.1 A Hello World Example . 131 30.2 An example with CGAL . 132 30.3 Handling Python objects and errors . 133 31 WSGI web programming 135 32 WSGI Web Programming 137 32.1 External Resources . 137 33 References 139 33.1 Language reference . 139 33.2 External links . 139 34 Authors 141 34.1 Authors of Python textbook . 141 35 Contributors 143 List of Figures 149 36 Licenses 153 36.1 GNU GENERAL PUBLIC LICENSE . 153 36.2 GNU Free Documentation License . 154 36.3 GNU Lesser General Public License . 154 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 Gudio 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 systems and has adopted it as the standard scripting language for its Integrated Planning System. Python is also extensively used by Google to implement many components of its Web Crawler and Search Engine & Yahoo! for managing its discussion groups. Python within itself is an interpreted programming language that is automatically compiled into bytecode before execution (the bytecode is then normally saved to disk, just as automatically, so that compilation need not happen again until and unless the source gets changed). It is also a dynamically typed language that includes (but does not require one to use) object oriented features and constructs. The most unusual aspect of Python is that whitespace is significant; instead of block delimiters (braces → "{}" in the C family of languages), indentation is used to indicate where blocks begin and end. For example, the following Python code can be interactively typed at an interpreter prompt, display the famous "Hello World!" on the user screen: >>> print "Hello World!" Hello World! Another great Python feature is its availability for all Platforms. Python can run on Microsoft Windows, Macintosh & all Linux distributions with ease. This makes the programs very portable, as any program written for one Platform can easily be used at another. Python provides a powerful assortment of built-in types (e.g., lists, dictionaries and strings), a number of built-in functions, and a few constructs, mostly statements. For example, loop constructs that can iterate over items in a collection instead of being limited to a simple range of integer values. Python also comes with a powerful standard library7, which includes hundreds of modules to provide routines for a wide variety of services including regular expressions8 and TCP/IP sessions. 1 http://en.wikibooks.org/wiki/Python 2 http://en.wikibooks.org/wiki/Computer%20programming%2FHighlevel 3 http://en.wikibooks.org/wiki/Computer%20programming%2FStructured% 20programming 4 http://en.wikibooks.org/wiki/Open%20Source 5 http://en.wikibooks.org/wiki/Python 6 http://en.wikibooks.org/wiki/Linux 7 http://en.wikibooks.org/wiki/Python%20Programming%2FStandard%20Library 8 Chapter 21 on page 99 3 Overview Python is used and supported by a large Python Community9 that exists on the Internet. The mailing lists and news groups10 like the tutor list11 actively support and help new python programmers. While they discourage doing homework for you, they are quite helpful and are populated by the authors of many of the Python textbooks currently available on the market. 9 http://www.python.org/community/index.html 10 http://www.python.org/community/lists.html 11 http://mail.python.org/mailman/listinfo/tutor 4 2 Getting Python In order to program in Python you need the Python interpreter. If it is not already installed or if the version you are using is obsolete, you will need to obtain and install Python using the methods below: 2.1 Python 2 vs Python 3 In 2008, a new version of Python (version 3) was published that was not entirely backward compatible. Developers were asked to switch to the new version as soon as possible but many of the common external modules are not yet (as.