Next / Previous / Contents / TCC Help System / NM Tech homepage


A reference guide to most of the common features of the Python programming language, version 2.7.

This publication is available in Web form and also as a PDF document. Please forward any comments to

Table of Contents

1. Introduction: What is Python?
2. Python 2.7 and Python 3.x
3. Starting Python
3.1. Using Python in Windows
3.2. Using Python in Linux
4. Line syntax
5. Names and keywords
6. Basic types
7. Numeric types
7.1. Type int: Integers
7.2. Type long: Extended-precision integers
7.3. Type bool: Boolean truth values
7.4. Type float: Floating-point numbers
7.5. Type complex: Imaginary numbers
8. Sequence types
8.1. Operations common to all the sequence types
9. Type str: Strings of 8-bit characters
9.1. String constants
9.2. Definition of “whitespace”
9.3. Methods on str values
9.4. The string .format() method
9.4.1. General form of a format code
9.4.2. The name part
9.4.3. The conversion part
9.4.4. The spec part
9.4.5. Formatting a field of variable length
9.5. The older string format operator
10. Type unicode: Strings of 32-bit characters
10.1. The UTF-8 encoding
11. Type list: Mutable sequences
11.1. Methods on lists
11.2. List comprehensions
12. Type tuple: Immutable sequences
13. The bytes type
13.1. Using the bytes type in 3.x conversion
14. The bytearray type
15. Types set and frozenset: Set types
15.1. Operations on mutable and immutable sets
15.2. Operations on mutable sets
16. Type dict: Dictionaries
16.1. Operations on dictionaries
16.2. Dictionary comprehensions
17. Type file: Input and output files
17.1. Methods on file objects
18. None: The special placeholder value
19. Operators and expressions
19.1. What is a predicate?
19.2. What is an iterable?
19.3. Duck typing, or: what is an interface?
19.4. What is the locale?
20. Basic functions
20.1. abs(): Absolute value
20.2. all(): Are all the elements of an iterable true?
20.3. any(): Are any of the members of an iterable true?
20.4. bin(): Convert to binary
20.5. bool(): Convert to Boolean
20.6. bytearray(): Create a byte array
20.7. chr(): Get the character with a given code
20.8. cmp(): Compare two values
20.9. complex(): Convert to complex type
20.10. dict(): Convert to a dictionary
20.11. divmod(): Quotient and remainder
20.12. enumerate(): Step through indices and values of an iterable
20.13. file(): Open a file
20.14. filter(): Extract qualifying elements from an iterable
20.15. float(): Convert to float type
20.16. format(): Format a value
20.17. frozenset(): Create a frozen set
20.18. hex(): Convert to base 16
20.19. int(): Convert to int type
20.20. input(): Read an expression from the user
20.21. iter(): Produce an iterator over a sequence
20.22. len(): Number of elements
20.23. list(): Convert to a list
20.24. long(): Convert to long type
20.25. map(): Apply a function to each element of an iterable
20.26. max(): Largest element of an iterable
20.27. min(): Smallest element of an iterable
20.28. next(): Call an iterator
20.29. oct(): Convert to base 8
20.30. open(): Open a file
20.31. ord(): Find the numeric code for a character
20.32. pow(): Exponentiation
20.33. range(): Generate an arithmetic progression as a list
20.34. raw_input(): Prompt and read a string from the user
20.35. reduce(): Sequence reduction
20.36. reversed(): Produce a reverse iterator
20.37. round(): Round to the nearest integral value
20.38. set(): Create an algebraic set
20.39. sorted(): Sort a sequence
20.40. str(): Convert to str type
20.41. sum(): Total the elements of a sequence
20.42. tuple(): Convert to a tuple
20.43. type(): Return a value's type
20.44. unichr(): Convert a numeric code to a Unicode character
20.45. unicode(): Convert to a Unicode string
20.46. xrange(): Arithmetic progression generator
20.47. zip(): Combine multiple sequences
21. Advanced functions
21.1. basestring: The string base class
21.2. callable(): Is this thing callable?
21.3. classmethod(): Create a class method
21.4. delattr(): Delete a named attribute
21.5. dir(): Display a namespace's names
21.6. eval(): Evaluate an expression in source form
21.7. execfile(): Execute a Python source file
21.8. getattr(): Retrieve an attribute of a given name
21.9. globals(): Dictionary of global name bindings
21.10. hasattr(): Does a value have an attribute of a given name?
21.11. id(): Unique identifier
21.12. isinstance(): Is a value an instance of some class or type?
21.13. issubclass(): Is a class a subclass of some other class?
21.14. locals(): Dictionary of local name bindings
21.15. property(): Create an access-controlled attribute
21.16. reload(): Reload a module
21.17. repr(): Representation
21.18. setattr(): Set an attribute
21.19. slice(): Create a slice instance
21.20. staticmethod(): Create a static method
21.21. super(): Superclass
21.22. vars(): Local variables
22. Simple statements
22.1. The assignment statement: name = expression
22.2. The assert statement: Verify preconditions
22.3. The del statement: Delete a name or part of a value
22.4. The exec statement: Execute Python source code
22.5. The global statement: Declare access to a global name
22.6. The import statement: Use a module
22.7. The pass statement: Do nothing
22.8. The print statement: Display output values
22.9. The print() function
23. Compound statements
23.1. Python's block structure
23.2. The break statement: Exit a for or while loop
23.3. The continue statement: Jump to the next cycle of a for or while
23.4. The for statement: Iteration over a sequence
23.5. The if statement: Conditional execution
23.6. The raise statement: Cause an exception
23.7. The return statement: Exit a function or method
23.8. The try statement: Anticipate exceptions
23.9. The with statement and context managers
23.10. The yield statement: Generate one result from a generator
24. def(): Defining your own functions
24.1. A function's local namespace
24.2. Iterators: Values that can produce a sequence of values
24.3. Generators: Functions that can produce a sequence of values
24.4. Decorators
25. Exceptions: Error signaling and handling
25.1. Definitions of exception terms
25.2. Life cycle of an exception
25.3. Built-in exceptions
26. Classes: Defining your own types
26.1. Old-style classes
26.1.1. Defining an old-style class
26.1.2. Instantiation of an old-style class: The constructor, .__init__()
26.1.3. Attribute references in old-style classes
26.1.4. Method calls in an old-style class
26.1.5. Instance deletion: the destructor, .__del__()
26.2. Life cycle of a new-style class
26.2.1. __new__(): New instance creation
26.2.2. Attribute access control in new-style classes
26.2.3. Properties in new-style classes: Fine-grained attribute access control
26.2.4. Conserving memory with __slots__
26.3. Special method names
26.3.1. Rich comparison methods
26.3.2. Special methods for binary operators
26.3.3. Unary operator special methods
26.3.4. Special methods to emulate built-in functions
26.3.5. __call__(): What to do when someone calls an instance
26.3.6. __cmp__(): Generalized comparison
26.3.7. __contains__(): The “in” and “not in” operators
26.3.8. __del__(): Destructor
26.3.9. __delattr__(): Delete an attribute
26.3.10. __delitem__(): Delete one item of a sequence
26.3.11. __enter__: Context manager initialization
26.3.12. __exit__: Context manager cleanup
26.3.13. __format__: Implement the format() function
26.3.14. __getattr__(): Handle a reference to an unknown attribute
26.3.15. __getattribute__(): Intercept all attribute references
26.3.16. __getitem__(): Get one item from a sequence or mapping
26.3.17. __iter__(): Create an iterator
26.3.18. __nonzero__(): True/false evaluation
26.3.19. __repr__(): String representation
26.3.20. __reversed__(): Implement the reversed() function
26.3.21. __setattr__(): Intercept all attribute changes
26.3.22. __setitem__(): Assign a value to one item of a sequence
26.4. Static methods
26.5. Class methods
27. pdb: The Python interactive debugger
27.1. Starting up pdb
27.2. Functions exported by pdb
27.3. Commands available in pdb
28. Commonly used modules
28.1. math: Common mathematical operations
28.2. string: Utility functions for strings
28.3. random: Random number generation
28.4. time: Clock and calendar functions
28.5. re: Regular expression pattern-matching
28.5.1. Characters in regular expressions
28.5.2. Functions in the re module
28.5.3. Compiled regular expression objects
28.5.4. Methods on a MatchObject
28.6. sys: Universal system interface
28.7. os: The operating system interface
28.8. stat: Interpretation of file status
28.9. os.path: File and directory interface
28.10. argparse: Processing command line arguments
28.10.1. Types of command line arguments
28.10.2. Overall flow of argument processing
28.10.3. The ArgumentParser() constructor
28.10.4. The ArgumentParser.add_argument() method
28.10.5. The ArgumentParser.parse_args() method
28.10.6. Other useful ArgumentParser methods