Why Python?Among the reasons Pythonistas (rabid fans of Python) love the language are that it does automatic data type conversions, so data overflow errors cannot happen. Many are forced to learn the language in order to maintain jacl Jython wsadmin scripts administering IBM Websphere servers. Google chose to build Google Apps running within the Google cloud at *.appspot.com. Even though Google enabled Java in 2009, some of the underlying code is still in Python. Others come to Python because of the power of the CMS Plone running on the free open-source Zope web application server; the lighter-weight Django, or the PushToTest SOA testing framework written in Python. Language HistoryThe Python language is not named after a reptile, but the British TV series "Monty Python's Flying Circus", which ran from 5 Oct 1969 to 1973 on BBC-1 and on BBC-2 (without Cleese) until 5 Dec 1974. The show featured surreal sketch comedy by many of Britain's most well-known commedians who went on to create "Monty Python and the Holy Grail" and other silly movies. They are: Graham Chapman, John Cleese, Terry Gilliam, Terry Jones, Michael Palin, and Eric Idle.The name of the Python IDE (that comes with Linux) is the acronymn IDLE. Versions to 1.2 were created during 1991-1995 principally by Guido van Rossum at the Stichting Mathematisch Centrum (CWI) in the Netherlands. This is noted in the first prompt when Python starts. — Guido van Rossum and Jelke de Boer, "Interactively Testing Remote Servers Using the Python Programming Language", CWI Quarterly, Volume 4, Issue 4 (December 1991), Amsterdam, pp 283-303. Versions to 1.6 during 1995-2001 were created by Guido at CNRI (Corporation for National Research Initiatives) in Reston, Virginia. Version 2.0 in 2000 was released for the first time via SourceForge by Guido's PythonLabs team at BeOpen.com. Version 2.1 and beyond (2001 on) become owned by the non-profit Python Software Foundation (PSF) modeled after the Apache Software Foundation. This happened after Guido's team moved to Digital Creations, which became Zope (which offers CMS and Intranets). Version 2.4 introduced the decimal data type for 28 digit precision with no rounding errors with money. Version 2.5 string concatenation was speeded up, making previous hacks unnecessary. Version 2.6 added the JSON library from simplyjson. But as of 6/2/09 windows binaries are no longer created for mod_python, requiring builds. So people have avoided this version. Python 3.x (also called Python 3000) is not backward compatible with 2.x Python source. |
Python Programming for the Absolute Beginner (Boston: Course PTR, 2003) by Michael Dawson
Python Cookbook at ActiveState Among published Success stories is use by Google, Disney, NASA, Industrial Light & Magic, and other cool places to work. |
Python Run Environments
Native Python runs on top of the OS. For tab completions, history recall, and other aids (as Aral notes), download iPython Jython implements the Python scripting language in Java, so it runs within Java (v1.4+) and thus can access Java libraries. Other implementations of Python include CPython, PyPy, IronPython, Pyrex, Psyco, etc. The Crimson Editor is Python-aware. |
Jython for Java Programmers (New Riders, Indianapolis) by Robert W. Bill Tips for Scripting Java with Jython by O'Reilly and Noel Rappin covers 11 specific features of Jython that Java programmers can be excited about. Within IBM's DeveloperWorks Java Zone, Developing Web services with Python can help you understand the workings of Jython development. |
Installers
Most Linux distributions come with Python installed. All Apple computers running Mac OS X come with Python. MacPython by Jack Jansen Computers from Hewlett-Packard/Compaq come with Python. Some of HP/Compaq's administrative tools are written in Python. MS Windows users need to download an installer from one of these locations:
ActivePython from ActiveState provides support with the paid Enterprise edition. There are differences among implementations, which may not fully implement all the standard python libraries.
By default: The Windows installer also adds the selected folder to the front of Windows System Environment Variable Path. Using the default folder (instead of the C:\bin folder) does clutter up my root folder more, but many apps that depend on it (such as twistd) look for that default folder name. Download and run Python Extensions for Windows (such as 5.15MB file pywin32-211.win32-py2.5.exe) for Python 2.5 by Mark Hammond To install Jython (without the IDLE IDE):
2. Download the Jython jar 3. Add the full path with file name to CLASSPATH (separated by a semicolon) 4. Execute the jar file and specify the install path (such as C:\bin\jython2.5.0) 5. Add that folder in your PATH environment variable. 6. Create a folder for your python source code (such as C:\projects\python). |
Jython Essentials (O'Reilly, March 21, 2002) by Samuele Pedroni, Noel Rappin
Python Pocket Reference (Beijing ; Sebastopol, CA: O'Reilly, 1998) by Mark Lutz
|
IDEs
ActiveState's support for Visual Python (integration with MS Visual Studio) was discontinued as of December 15, 2005 in favor of the Komodo IDE.
PyDev IDE for Eclipse EasyGUI |
Starting the Python EnvironmentInteractive Shell Start-upOn Windows machines, Python can be started from Start > Programs > Active State 2.x > Python Interactive Shell, which invokes C:\Python25\python.exe.Alternately, running the installer (msi file) puts one of these folders in your machine's PATH environment variable, so python.exe can be started from any folder from the Windows Run command prompt.
Jython users:
Start with HelpWhen Python is started with the -h (help) flag:usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -c cmd : program passed in as string (terminates option list) -d : debug output from parser (also PYTHONDEBUG=x) -E : ignore environment variables (such as PYTHONPATH) -h : print this help message and exit -i : inspect interactively after running script, (also PYTHONINSPECT=x) and force prompts, even if stdin does not appear to be a terminal -m mod : run library module as a script (terminates option list) -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x) -OO : remove doc-strings in addition to the -O optimizations -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew -S : don't imply 'import site' on initialization -t : issue warnings about inconsistent tab usage (-tt: issue errors) -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x) see man page for details on internal buffering relating to '-u' -v : verbose (trace import statements) (also PYTHONVERBOSE=x) -V : print the Python version number and exit -W arg : warning control (arg is action:message:category:module:lineno) -x : skip first line of source, allowing use of non-Unix forms of #!cmd file : program read from script file - : program read from stdin (default; interactive mode if a tty) arg ...: arguments passed to program in sys.argv[1:] Other environment variables: PYTHONSTARTUP: file executed on interactive startup (no default) PYTHONPATH : ';'-separated list of directories prefixed to the default module search path. The result is sys.path. PYTHONHOME : alternate Upon start-up, python executes the contents of a file identified by the PYTHONSTARTUP environment variable installed by the msi file. Invocation Alternatives
Python can also be invoked with commands followed by "-c", or modules followed by "-m". To find the full path of Python's "site-packages" folder:
>>> from distutils.sysconfig import get_python_lib >>> print get_python_lib() C:\Python27\Lib\site-packages This path is where packages like Django (and its django-admin.py) should be installed: C:\Python27\Lib\site-packages\Django\bin Wiki how Installing Django on Dreamhost |
The >>> Prompt for the Dynamically and Strongly Typed
The >>> prompt shown by the standard interpreter or
Python does not recognize the "exit" command used in Windows Run command windows. But since it is a common mistake, when you type exit the reply is:
To exit, press Ctrl+Z (^Z) and then Enter/Return key.
C and Java are "compiled" languages.
So Python's interactive Mode prompt >>> can be used like a calculator, where commands typed in are immediately executed.
2 >>> print "2 cubed is",(2 * 2 * 2) 2 cubed is 8 >>> print "% cubed is" % (2,(2 * 2 * 2)) 2 cubed is 8 Unlike languages such as C and Java, you don't need to declare variables with their datatypes before using them. Python does not require variable or argument declarations. This is because Python (and VBScript) are called "dynamically typed" languages since Python figures out at time of execution what datatype a variable is when it is first assigned, rather than at time of compilation (such as "statically typed" languages like C and Java). The largest integer on a 64-bit system: >>> import sys >>> print sys.maxint 9223372036854775807 Python automatically promotes integers to float. Python is called a "strongly typed" languages becuase it always enforces types. Unlike the "weakly typed" VBScript language, within Python (and Java), you cannot (without any explicit conversion) concatenate the string '12' and the integer 3 to get the string '123', then treat that as the integer 123. With Python, if you try to treat an integer as a string without first explicitly converting it, you will get a TypeError.
|
Writing the 33 programs of the PythonChallenge is a fun and way to learn. pytut.infogami.com offers Python official docs in a community-modifiable `Wiki.Python aims to offer what Perl and Microsoft script offers. Python Cookbook (O'Reilly, 2002) by Alex Martelli and David Ascher
Learning Python, 2nd Ed. (Beijing ; Sebastopol, CA: O'Reilly, 2003) 1st ed (1999) by Mark Lutz, David Ascher Beginning Python: From Novice to Professional (2005) by Magnus Hetland comes highly recommended. Python in a Nutshell (O'Reilly) by Alex Martelli presents a good quick reference. Python Essential Reference (2nd Edition) by David Beazley Dive Into Python by Mark Pilgrim Also online at Dive Into Python (good coverage in excellent prose).
Sams Teach Yourself Python in 24 Hours (Indianapolis, Ind.: Sams Publishing, 2000) by Ivan Van Laningham
Making Use of Python (NY: Wiley, 2002) by Rashi Gupta
|
Python Source Code BasicsLike the Monty Python show, which "offered savage broadsides against the pomposity and repression of the British establishment." the Python language aims to offer an improvement to C and Java. We follow the Python Code Style Guide (expanded in a mediaWiki version). The Main Module Upside DownPython begins by executing code such as this usually at the bottom of the main module's source code file: if __name__=="__main__": main_logic()
Comments can contain doc strings. Doc strings can be retrieved using the print docstring.__doc__ attribute. The backward slash is Python's line continuation character.
Instead of semicolons at the end of each expression (as with C or Java), like VBScript
Python uses {} curly braces to define dictionaries.
To continue a line, add a \ back slash and indent the following line. The # hash (or pound sign as some call it) begins comments (as with C). This can't follow a continuation character on the same line. "..." elipses (3 periods) precedes each multi-line construct. Python's high-level data types allow complex operations to be expressed in a single statement. The Python community has a saying: "Python comes with batteries included." Python is extensible: functions can be added by adding modules. The pass statement does nothing.
A leading '0' (zero) indicates octal, '0x' indicates a hex number, "_" (the underline character) stands in for the last variable used (read-only). "\" backslash character at the end of a line is used to specify continuation to the next line. "\n" is an escape character for "next line" (not necessary between triple quotes). "\\" is thus necessary to specify back-slash separators between folders in a directory path. "+" is used to concantenate strings (rather than the amersand & in C). Strings can be encased between either double or single quotes. Unlike C, assignments are not allowed within expressions.
|
Importing Python Source Files
When naming python modules, do not use special characters such as a period or a dash, When a module is imported for the first time (or when the source is more recent than the current compiled file) Python parses and translates it into a bytecode files suffixed with .pyc in the same folder as the source file. Python scripts are made into an executable Windows script when its named with a .cmd suffix and contains this first line:
Source StructureJim Roskind suggests ordering various types of code in this order within modules:Intermingling SourceTo intermingle Python and C code to increase performance:
|
Built-in Names (Modules, Functions, Variables)
Adding name "L" to the local namespace, making it refer to an empty list object.
>>> L = ['a',1] # notice mixed types >>> L.append('b') >>> L ['a',1,'b'] Get an alphabetized list of the names containing the instance attributes and methods and attributes defined by its class:
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] Use a method:
>>> L ['a',1,'b'] Empty dictionary:
>>> dir(a) ['clear', 'copy', 'get', 'has_key', 'items', 'keys', 'update', 'values'] Double underlines on both sides of a name designate a built-in system name. Double leading underscores and no trailing underscores designate that the attribute can be automatically mangled by Python with the simple class name in order to avoid name collisions should subclasses inadvertently contain attributes with the same name. However, this makes __getattr__() less convenient. A single leading underscore to a method or attribute designates it as private. This only discourages direct access externally to the class but does not prevent access. To load python's built-in libraries and list it:
>>> dir(__builtin__) ['ArithmeticError', 'AssertionError', 'AttributeError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'IOError', 'ImportError', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'OverflowWarning', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', 'abs', 'apply', 'basestring', 'bool', 'buffer', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip'] |
|
Import sys and PYTHONPATH
Python scripts import modules only from directories specified in the PYTHONPATH environment variable. On Unix, this is usually .:/usr/local/lib/python. Internally, this is variable sys.path, which can be changed with code such as (on a Unix machine):
>>> sys.path ['', '/usr/local/lib/python2.2', '/usr/local/lib/python2.2/plat-linux2', '/usr/local/lib/python2.2/lib-dynload', '/usr/local/lib/python2.2/site-packages', '/usr/local/lib/python2.2/site-packages/PIL', '/usr/local/lib/python2.2/site-packages/piddle'] On a Windows machine:
>>> sys.path
['', 'C:\\WINDOWS\\system32\\python24.zip', 'C:\\Documents and Settings\\W', 'C:\\Python24\\DLLs', 'C:\\Python24\\lib', 'C:\\Python24\\lib\\plat-win', 'C:\\Python24\\lib\\lib-tk', 'C:\\Python24', 'C:\\Python24\\lib\\site-packages', 'C:\\Python24\\lib\\site-packages\\win32', 'C:\\Python24\\lib\\site-packages\\win32\\lib', 'C:\\Python24\\lib\\site-packages\\Pythonwin'] It is good practice to import modules in this order: After you change a module, force rereading:
>>> reload(modname) This does not affect modules which use: from modname import * So avoid using the above. They also clutter the namespace. Also avoid circular imports ... Arguments are passed by assignment in Python. To issue a prompt and accept an input from the user:
|
Dive Into Python (Apress, July 2004) by Mark Pilgrim (creator of the pyGoogle Google API wrapper) is available as an online book
|
Third-Party Class Libraries |
GUI LibrariesPython's de-facto standard for the most commonly usedTk originated from Sun Labs. Today it's available on most Unix and Macintosh platforms. Since release 8.0, a dll provides Microsoft Windows XP native look and feel. Tkinter consists of, among many modules, Tkconstants and the Tk low-level interface binary module _tkinter (which should never be used directly by application programmers). Tk is event driven |
DocumentationFollow PEP 257 Docstring conventions and styles . The first line should always be a short, concise summary of the object's purpose. For brevity, it should not explicitly state the object's name or type, since these are available by other means (except if the name happens to be a verb describing a function's operation). This line should begin with a capital letter and end with a period. If there are more lines in the documentation string, the second line should be blank, visually separating the summary from the rest of the description. The following lines should be one or more paragraphs describing the object's calling conventions, its side effects, etc. If you mark-up your source in structured-text docstrings format, you can use
pythondoc to generate XML with indexes. | Join to get daily digest emails from Jython User's Group Python Developer's Handbook (Indianapolis, Ind.: Pearson Education, 2001) by André dos Santos Lessa has errors Game Programming With Python, Lua, and Ruby (Boston, MA Course PTR, 2004) by Tom Gutschmidt
Numerical Methods in Engineering With Python (New York: Cambridge University Press, 2005) by Tom Gutschmidt
|
Compiling
__main__ modules don't become .pyc modules. But you can force compile with:
>>> py_compile.compile('xxx.py') .pyc files are written to the same location as .py file unless overrided with the optional parameter -cfile. To compile all files in a directory or directories:
You will be prompted for the directory path.
|
Unit Testing and Debugging Python Programs
First, use a static analysis tool to find syntactic bugs in Python source code. These tools also warns about code complexity and style.
pdb is the default console debugger module. The doctest module finds examples in the docstrings for a module and runs them, comparing the output with the expected output given in the docstring. The unittest module PyUnit.sourceforge.net testing framework is modelled after Kent Beck's JUnit and smalltalk testing framework.
|
|
IDEs (Integrated Development Environments)
Those spending a lot of time working on Python appreciate the time savings and features of an IDE.
|
Utilities For Python Source Code
|
Working Folders
>>> import os # POSIX module >>> os.listdir('.') To create a new folder (and any intermediate directories which don't exist): >>> os.makedirs( path ) To remove a directory (and any intermediate directories which are empty): >>> os.removedirs( path ) To remove a directory: >>> os.rmdir() To delete an entire directory tree and its contents: >>> shutil.rmtree() The shutil module also has copyfile, copytree, rmtree, and other file functions.
|
|
Working Files
>>> f = open( '/tmp/sx', 'r+') To read an entire file opened by the statement above: >>> f.read() To read n bytes from a pipe: >>> f.readline( n ) To open a file using a high-level function to get a small integer file handle for write): >>> f = os.popen( '/tmp/sx', 'w') To write string s with a newline break or list L: >>> f.write( 'wow\n' ) >>> f.writelines( L ) To redirect print statements to a file rather the stdout screen: >>> import sys >>> temp = sys.stdout >>> sys.stdout = open('log.txt','a') >>> print x # to file >>> std.stdout = temp >>> print a # to stdout To truncate a file from the current offset seek position: >>> f.truncate( offset ); To rename a file: >>> os.rename( old_path, new_path) To delete a single file: >>> os.remove(filename)
|
|
Database Access>>> import odbchelper >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"} >>> print odbchelper.buildConnectionString(params) server=mpilgrim;uid=sa;database=master;pwd=secret
|
|
Accessing ConsoleTo request input of an integer from user: >>> int( raw_input('Enter a number:')) |
Performance AlgorithmsPyStone is the benchmark currently used as a highlevel check on performance availability from the python users' perspective. The default loop count is 50,000 iterations, which takes about 5 seconds on the developer's baseline machine. performance testing with a pystone measurement decorator Use function annotation to track elapsed execution time To see whether a new object has been spawned, obtain its internal identifier: >>> x="me" >>> id(x) 13404401 >>> z=x >>> id(z) 13404401
|
|
Interfaces |
|
Sub classes |
Object Identifier Naming & Duration
The structure of names assigned is crucial to developer efficiency by reducing refactoring effort during construction and subsequent maintenance. We of course follow standards enforced by the compiler:
Reference to global address space are explicitly defined. A tuple of global objects is passed to a "set" function in the imported source file. (In this example are Admin* variables among wsadmin to admin IBM Websphere): import efgh efgh.setAdminRefs((AdminConfig, \ AdminControl, AdminTask, AdminApp)) efgh.listServers()Within file efgh.py: def setAdminRefs(adminTuple): global AdminConfig, AdminControl global AdminTask, AdminApp (AdminConfig, AdminControl, AdminTask, AdminApp) \ = adminTuple def listServers(): print AdminConfig.list("Server") |
Lists
0 starts is the first number, as with C and Java.
Think of seq[-n] as the same as seq[len(seq)-n]. S[:-1] is all of the string except for its last character — useful for removing the trailing newline from a string. It's more efficient to use string methods foo.startswith('bar') and ''.endswith() rather than foo[:3] == 'bar'. Tuples - Dictionaries - hashTuples are small collections of related data operated on as a group. Tuples are immutable: once a tuple has been created, it can't be replaced with a new value. Tuples can be used as keys because only immutable elements can be used as dictionary keys, which are implemented as resizable hash tables. Its members may be of different types.>>> d1 = {'a':1, 'b':2, 'c':3} >>> d1['a'] = 4 >>> d1['a'] 4 Dictionaries work by computing a hash code for each key stored in the dictionary using the hash() built-in function. The hash code varies widely depending on the key. For example, the string "Python" hashes to -539294296 while "python", a string that differs by a single character, hashes to 1142331976. This hash code is then used to calculate a location in an internal array where the value will be stored. Assuming that you're storing keys that all have different hash values, this means that dictionaries take constant time " O(1), in computer science notation " to retrieve a key. It also means that no sorted order of the keys is maintained, and traversing the array as the .keys() and .items() do will output the dictionary's content in some arbitrary jumbled order. In Python, strings are immutable. Once defined, they can't be changed. However, individual elements of a list can be redefined for reference directly: >>> s = "Hello world" >>> a = list(s) >>> print a ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] >>> L[1]="h" To turn an object back into a string (so it can be written to a file): >>> ''.join(a) 'Hello there!' Instead of '' above, ',' causes a comma to be inserted between each item. It's faster to use the array module: >>> import array >>> a = array.array('c', s) >>> print a array('c', 'Hello, world') >>> a[0] = 'y' ; print a array('c', 'yello world') >>> a.tostring() 'yello, world' You can use slice notation to insert a copy of itself at the beginning of list b:
With slice, starting and ending locations (starting from zero): two indices separated by a colon. Negative numbers count from the right. With print, a space is automatically inserted between items. And a newline is inserted before the next prompt if the last line was not completed. One neat Python trick is multiplication of strings:
HelloHelloHello |
|
Developer Resources |
Style Guides: "Pythonic" syntax NumbersPython has no ternary operator such as a?b:c in C. Instead, Python has borrowed Smalltalk's lambda expressions
|
Handing Large Numbers with Epsilon
str(23) yields 23.
When comparing two very large or small numbers, a regular == operator would fail because of small differences.
0.20000000000000001 So check whether the difference between the two numbers is less than a certain threshold: the epsilon. epsilon = 0.0000000000001 # Tiny allowed error expected_result = 0.4 if expected_result-epsilon <= computation() <= expected_result+epsilon: |
|
Related Topics:
Keyboard Shortcuts
Project Software
Project Central
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page
Thank you! |