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
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. Input and output
9.1. Reading files
9.2. File positioning for random-access devices
9.3. Writing files

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.