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

Abstract

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

This publication is available in Web form and also as a PDF document. Please forward any comments to tcc-doc@nmt.edu.

Table of Contents

1. Introduction: What is Python?
2. Starting Python
2.1. Using Python in Windows
2.2. Using Python in Linux
3. Line syntax
4. Reserved words
5. Basic types
6. Numeric types
6.1. Type int: Integers
6.2. Type long: Extended-precision integers
6.3. Type bool: Boolean truth values
6.4. Type float: Floating-point numbers
6.5. Type complex: Imaginary numbers
7. Sequence types
7.1. Operations common to all the sequence types
7.2. Type str: Strings of 8-bit characters
7.2.1. String constants
7.2.2. The string format operator
7.2.3. String formatting from a dictionary
7.2.4. Definition of “whitespace”
7.2.5. Methods on str values
7.3. Type unicode: Strings of 32-bit characters
7.3.1. The UTF-8 encoding
7.4. Type list: Mutable sequences
7.4.1. Methods on lists
7.4.2. List comprehensions
7.5. Type tuple: Immutable sequences
8. Types set and frozenset: Set types
8.1. Operations on mutable and immutable sets
8.2. Operations on mutable sets
9. Type dict: Dictionaries
9.1. Operations on dictionaries
10. Type file: Input and output files
10.1. Methods on file objects
11. None: The special placeholder value
12. Operators and expressions
12.1. What is a predicate?
13. Basic functions
13.1. abs(): Absolute value
13.2. all(): Are all the elements of a sequence true?
13.3. any(): Are any of the members of a sequence true?
13.4. bool(): Convert to Boolean
13.5. chr(): Get the character with a given code
13.6. cmp(): Compare two values
13.7. complex(): Convert to complex type
13.8. dict(): Convert to a dictionary
13.9. divmod(): Quotient and remainder
13.10. enumerate(): Step through indices and values of a sequence
13.11. file(): Open a file
13.12. filter(): Extract qualifying elements from a sequence
13.13. float(): Convert to float type
13.14. frozenset(): Create a frozen set
13.15. hex(): Convert to base 16
13.16. int(): Convert to int type
13.17. iter(): Produce an iterator over a sequence
13.18. len(): Number of elements
13.19. list(): Convert to a list
13.20. long(): Convert to long type
13.21. map(): Apply a function to each element of a sequence
13.22. max(): Largest element of a sequence
13.23. min(): Smallest element of a sequence
13.24. oct(): Convert to base 8
13.25. open(): Open a file
13.26. ord(): Find the numeric code for a character
13.27. pow(): Exponentiation
13.28. range(): Generate an arithmetic progression as a list
13.29. raw_input(): Prompt and read a string from the user
13.30. reduce(): Sequence reduction
13.31. reversed(): Produce a reverse iterator
13.32. round(): Round to the nearest integral value
13.33. set(): Create an algebraic set
13.34. sorted(): Sort a sequence
13.35. str(): Convert to str type
13.36. sum(): Total the elements of a sequence
13.37. tuple(): Convert to a tuple
13.38. type(): Return a value's type
13.39. unichr(): Convert a numeric code to a Unicode character
13.40. unicode(): Convert to a Unicode string
13.41. xrange(): Arithmetic progression generator
13.42. zip(): Combine multiple sequences
14. Advanced functions
14.1. basestring: The string base class
14.2. callable(): Is this thing callable?
14.3. classmethod(): Create a class method
14.4. delattr(): Delete a named attribute
14.5. dir(): Display a namespace's names
14.6. eval(): Evaluate an expression in source form
14.7. execfile(): Execute a Python source file
14.8. getattr(): Retrieve an attribute of a given name
14.9. globals(): Dictionary of global name bindings
14.10. hasattr(): Does a value have an attribute of a given name?
14.11. id(): Unique identifier
14.12. isinstance(): Is a value an instance of some class or type?
14.13. issubclass(): Is a class a subclass of some other class?
14.14. locals(): Dictionary of local name bindings
14.15. property(): Create an access-controlled attribute
14.16. reload(): Reload a module
14.17. repr(): Representation
14.18. setattr(): Set an attribute
14.19. slice(): Create a slice instance
14.20. staticmethod(): Create a static method
14.21. super(): Superclass
14.22. vars(): Local variables
15. Simple statements
15.1. The assignment statement: name = expression
15.2. The assert statement: Verify preconditions
15.3. The del statement: Delete a name or part of a value
15.4. The exec statement: Execute Python source code
15.5. The global statement: Declare access to a global name
15.6. The import statement: Use a module
15.7. The pass statement: Do nothing
15.8. The print statement: Display output values
16. Compound statements
16.1. Python's block structure
16.2. The break statement: Exit a for or while loop
16.3. The continue statement: Jump to the next cycle of a for or while
16.4. The for statement: Iteration over a sequence
16.5. The if statement: Conditional execution
16.6. The raise statement: Cause an exception
16.7. The return statement: Exit a function or method
16.8. The try statement: Anticipate exceptions
16.9. The yield statement: Generate one result from a generator
17. def(): Defining your own functions
17.1. A function's local namespace
17.2. Iterators: Values that can produce a sequence of values
17.3. Generators: Functions that can produce a sequence of values
18. Exceptions: Error signaling and handling
18.1. Definitions of exception terms
18.2. Life cycle of an exception
18.3. Built-in exceptions
19. Classes: Defining your own types
19.1. Old-style classes
19.1.1. Defining an old-style class
19.1.2. Instantiation of an old-style class: The constructor, .__init__()
19.1.3. Attribute references in old-style classes
19.1.4. Method calls in an old-style class
19.1.5. Instance deletion: the destructor, .__del__()
19.2. Life cycle of a new-style class
19.2.1. __new__(): New instance creation
19.2.2. Attribute access control in new-style classes
19.2.3. Properties in new-style classes: Fine-grained attribute access control
19.2.4. Conserving memory with __slots__
19.3. Special method names
19.3.1. Rich comparison methods
19.3.2. Special methods for binary operators
19.3.3. Unary operator special methods
19.3.4. Special methods to emulate built-in functions
19.3.5. __call__(): What to do when someone calls an instance
19.3.6. __cmp__(): Generalized comparison
19.3.7. __contains__(): The “in” and “not in” operators
19.3.8. __del__(): Destructor
19.3.9. __delattr__(): Delete an attribute
19.3.10. __delitem__(): Delete one item of a sequence
19.3.11. __getattr__(): Handle a reference to an unknown attribute
19.3.12. __getattribute__(): Intercept all attribute references
19.3.13. __getitem__(): Get one item from a sequence or mapping
19.3.14. __iter__(): Create an iterator
19.3.15. __nonzero__(): True/false evaluation
19.3.16. __repr__(): String representation
19.3.17. __setattr__(): Intercept all attribute changes
19.3.18. __setitem__(): Assign a value to one item of a sequence
19.4. Static methods
19.5. Class methods
20. pdb: The Python interactive debugger
20.1. Starting up pdb
20.2. Functions exported by pdb
20.3. Commands available in pdb
21. Commonly used modules
21.1. math: Common mathematical operations
21.2. string: Utility functions for strings
21.3. random: Random number generation
21.4. time: Clock and calendar functions
21.5. re: Regular expression pattern-matching
21.5.1. Characters in regular expressions
21.5.2. Functions in the re module
21.5.3. Compiled regular expression objects
21.5.4. Methods on a MatchObject
21.6. sys: Universal system interface
21.7. os: The operating system interface
21.8. stat: Interpretation of file status
21.9. os.path: File and directory interface