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

Abstract

A tutorial for the Python programming language, release 2.7.

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 method
4. Sequence types
4.1. Functions and operators for sequences
4.2. Indexing the positions in a sequence
4.3. Slicing sequences
4.4. Sequence methods
4.5. List methods
4.6. The range() function: creating arithmetic progressions
4.7. One value can have multiple names
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, as of Python version 2.7. 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 ProgramsPython 2.7IDLE (Python GUI). You will see something like this:

  • For Linux or MacOS, from a shell prompt (such as “$” for the bash shell), type:

    python
    

    You will see something like this:

    $ python
    Python 2.7.1 (r271:86832, Apr 12 2011, 16:15:16) 
    [GCC 4.6.0 20110331 (Red Hat 4.6.0-2)] 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. (That's why we sometimes refer to conversational mode as “calculator mode”.) For example:

>>> 1+1
2
>>>

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