# Python Tutorial

Guido van Rossum
Corporation for National Research Initiatives (CNRI)
1895 Preston White Drive, Reston, Va 20191, USA
E-mail: `guido@CNRI.Reston.Va.US`, `guido@python.org`

December 31, 1997
Release 1.5

Copyright © 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands.

All Rights Reserved

Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI or Corporation for National Research Initiatives or CNRI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission.

While CWI is the initial source for this software, a modified version is made available by the Corporation for National Research Initiatives (CNRI) at the Internet address ftp://ftp.python.org.

STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

### Abstract:

Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python's elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python web site, ``http://www.python.org`', and can be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.

The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension language for customizable applications.

This tutorial introduces the reader informally to the basic concepts and features of the Python language and system. It helps to have a Python interpreter handy for hands-on experience, but all examples are self-contained, so the tutorial can be read off-line as well.

For a description of standard objects and modules, see the Python Library Reference document. The Python Reference Manual gives a more formal definition of the language. To write extensions in C or C++, read the Extending and Embedding and Python/C API manuals. There are also several books covering Python in depth.

This tutorial does not attempt to be comprehensive and cover every single feature, or even every commonly used feature. Instead, it introduces many of Python's most noteworthy features, and will give you a good idea of the language's flavor and style. After reading it, you will be able to read and write Python modules and programs, and you will be ready to learn more about the various Python library modules described in the Python Library Reference.

- Contents
- 1 Whetting Your Appetite
  - 1.1 Introduction
  - 1.2 Where From Here
- 2 Using the Python Interpreter
  - 2.1 Invoking the Interpreter
    - 2.1.1 Argument Passing
    - 2.1.2 Interactive Mode
  - 2.2 The Interpreter and its Environment
    - 2.2.1 Error Handling
    - 2.2.2 Executable Python scripts
    - 2.2.3 The Interactive Startup File
- 3 An Informal Introduction to Python
  - 3.1 Using Python as a Calculator
    - 3.1.1 Numbers
    - 3.1.2 Strings
    - 3.1.3 Lists
  - 3.2 First Steps Towards Programming
- 4 More Control Flow Tools
  - 4.1 If Statements
  - 4.2 For Statements
  - 4.3 The `range()` Function
  - 4.4 Break and Continue Statements, and Else Clauses on Loops
  - 4.5 Pass Statements
  - 4.6 Defining Functions
  - 4.7 More on Defining Functions
    - 4.7.1 Default Argument Values
    - 4.7.2 Keyword Arguments
    - 4.7.3 Arbitrary Argument Lists
- 5 Data Structures
  - 5.1 More on Lists
    - 5.1.1 Functional Programming Tools
  - 5.2 The `del` statement
  - 5.3 Tuples and Sequences
  - 5.4 Dictionaries
  - 5.5 More on Conditions
  - 5.6 Comparing Sequences and Other Types
- 6 Modules
  - 6.1 More on Modules
    - 6.1.1 The Module Search Path
    - 6.1.2 ``Compiled'' Python files
  - 6.2 Standard Modules
  - 6.3 The `dir()` function
- 7 Input and Output
  - 7.1 Fancier Output Formatting
  - 7.2 Reading and Writing Files
    - 7.2.1 Methods of file objects
    - 7.2.2 The pickle module
- 8 Errors and Exceptions
  - 8.1 Syntax Errors
  - 8.2 Exceptions
  - 8.3 Handling Exceptions
  - 8.4 Raising Exceptions
  - 8.5 User-defined Exceptions
  - 8.6 Defining Clean-up Actions
- 9 Classes
  - 9.1 A word about terminology
  - 9.2 Python scopes and name spaces
  - 9.3 A first look at classes
    - 9.3.1 Class definition syntax
    - 9.3.2 Class objects
    - 9.3.3 Instance objects
    - 9.3.4 Method objects
  - 9.4 Random remarks
  - 9.5 Inheritance
    - 9.5.1 Multiple inheritance
  - 9.6 Private variables through name mangling
  - 9.7 Odds and ends
    - 9.7.1 Exceptions Can Be Classes
- 10 What Now?
- 11 Recent Additions as of Release 1.1
  - 11.1 Lambda Forms
  - 11.2 Documentation Strings
- A Interactive Input Editing and History Substitution
  - A.0.1 Line Editing
  - A.0.2 History Substitution
  - A.0.3 Key Bindings
  - A.0.4 Commentary
- About this document ...