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

Abstract

A tutorial for the Python programming language.

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
1.1. Starting Python in conversational mode
2. Python's numeric types
2.1. Basic numeric operations
2.2. The assignment statement
2.3. More mathematical operations
3. Character string basics
3.1. String literals
3.2. Indexing strings
3.3. String methods
3.4. The string format operator
4. Sequence types
4.1. Functions and operators for sequences
4.2. Indexing the positions in a sequence
4.3. Slicing sequences
4.4. List methods
4.5. The range() function: creating arithmetic progressions
4.6. A list surprise
5. Dictionaries
5.1. Operations on dictionaries
5.2. Dictionary methods
5.3. A namespace is like a dictionary
6. Branching
6.1. Conditions and the bool type
6.2. The if statement
6.3. A word about indenting your code
6.4. The for statement: Looping
6.5. The while statement
6.6. Special branch statements: break and continue
7. How to write a self-executing Python script
8. def: Defining functions
8.1. return: Returning values from a function
8.2. Function argument list features
8.3. Keyword arguments
8.4. Extra positional arguments
8.5. Extra keyword arguments
8.6. Documenting function interfaces
9. Using Python modules
9.1. Importing items from modules
9.2. Import entire modules
9.3. A module is a namespace
9.4. Build your own modules
10. Input and output
10.1. Reading files
10.2. File positioning for random-access devices
10.3. Writing files
11. Introduction to object-oriented programming
11.1. A brief history of snail racing technology
11.2. Scalar variables
11.3. Snail-oriented data structures: Lists
11.4. Snail-oriented data structures: A list of tuples
11.5. Abstract data types
11.6. Abstract data types in Python
11.7. class SnailRun: A very small example class
11.8. Life cycle of an instance
11.9. Special methods: Sorting snail race data

1. Introduction

This document contains some tutorials for the Python programming language. These tutorials accompany the free Python classes taught by the New Mexico Tech Computer Center. Another good tutorial is at the Python website.

1.1. Starting Python in conversational mode

This tutorial makes heavy use of Python's conversational mode. When you start Python in this way, you will see an initial greeting message, followed by the prompt “>>>”.

  • On a TCC workstation in Windows, from the Start menu, select All ProgramsActiveState ActivePython 2.5Python Interactive Shell. You will see something like this:

  • For Linux or MacOS, from a shell prompt, type:

    python
    

    You will see something like this:

    -bash-3.1$ python
    Python 2.4.2 (#1, Feb 12 2006, 03:59:46) 
    [GCC 4.1.0 20060210 (Red Hat 4.1.0-0.24)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

When you see the “>>>” prompt, you can type a Python expression, and Python will show you the result of that expression. This makes Python useful as a desk calculator. For example:

>>> 1+1
2
>>>

Each section of this tutorial introduces a group of related Python features.