Introduction to Python


What is Python?


History


Guido van Rossum - Career


Monty Python

Named after Monty Python comedy show, not the snake. "Oh, I'm a lumberjack and I'm okay..."


Where do I get Python?

If your running a Linux distribution it is most likely already installed.


Python Licensing


Why use Python?


Python Style Guide

This essay on Python style covers:


Indentation rules

More on Python indentation:

Python uses block indentation, this is the way Python works. It allows for a clean consistent indenting style.

As a side note, if you are a vim/gvim user this will sequence will fix any indentation issues with Python:

set ts=4   (sets tabstops to 4)
set et     (expands tabs to spaces)
:%retab!   (enter command mode, retabify)
:w         (enter command mode, write file)

Python Terminology


Online Python book index


Python language books


Python reference books


Books on Python related topics


Online Documentation


Guido's Essays


USENET


Code Repository - Vaults of Parnassus


Additional Documentation


python man page

man python

Starting the interpreter

Open a xterm, or cmd window on Windows:

$ python
Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "so this is python"
so this is python
>>>
$

Exiting the interpreter


Running Python from the command line

python script_name.py

Use the shebang line, put this line as the first line of the python script (source file).

#!/usr/bin/python

Make sure the location of the python interpreter is correct on your system, this can be done by executing:

which python

Make the python script executable:

chmod 755 script_name.py

An alternative shebang line

Alternatively the env program can be used.

#!/usr/bin/env python

Running Python under Windows


Idle Development Enviroment


Idle Shell

idle shell

Idle Editor

idle editor

Idle Class Browser

idle class browser

Python docstrings

import sys

class DocStringExample:

    def hello(self):
        """This method prints out a hello message"""

        print "Hello World"
        return

if __name__ == '__main__':

    # create the object
    ds = DocStringExample()
    # print the documentation for a method

    print ds.hello.__doc__
    # run the method
    ds.hello()
    # exit the program
    sys.exit(0)

Program output:

This method prints out a hello message
Hello World

pydoc

pydoc <module_name>

Displays documenation for module sys.

pydoc -w <module_name>

Writes the documenation to disk in html format.

pydoc -k <search_item> <sys>

Performs a search of help documentation.

pydoc -p 1234 <module_name>

Starts a webserver on http://localhost:1234

pydoc -g

Brings up a Tkinter interface for searching, and starts a http server.

Also see: pythondoc


ply - the Lexx and Yacc of Python


Crypto Toolkit


Python Imaging Library


Graphical Toolkits for Python

List of graphical toolkits


Visual Interface Builders


Stani's Python Editor (SPE)

spe screenshot

Python Programmers Editor (PyPE)

pype screenshot

Eric3 IDE

eric3 screenshot

Komodo IDE (ActiveState)


Literate Editor with Outlines (Leo)

Also see:

Leo Screenshot

leo screenshot

Python Packaging

Packages that are distributed for use with Python typically come with at file named 'setup.py'. Change to the subdirectory where the package was decompressed and run the setup.py script:

python setup.py install

ctypes

ctypes is a foreign function library for Python. It provides C compatible data types, and allows to call functions in dlls/shared libraries. It can be used to wrap these libraries in pure Python.


Jython

From the Jython website:

Jython is an implementation of the high-level, dynamic, object-oriented language Python written in 100% Pure Java, and seamlessly integrated with the Java platform. It thus allows you to run Python on any Java platform.


Iron Python

From the above site:

IronPython is a new implementation of the Python programming language running on .NET. It supports an interactive console with fully dynamic compilation. It is well integrated with the rest of the .NET Framework and makes all .NET libraries easily available to Python programmers, while maintaining full compatibility with the Python language.


Audio Programs

Blogs


BuildBot - Build Automation using Python

BuildBot Screenshot:

biuldbot screenshot

Python PDF toolkit


PythonPoint

From the ReportLab website:

<frame x="160" y="72" width="600" height="468"

    leftmargin="36" rightmargin="36">
    <para style='Heading1'>
        Welcome to PythonPoint
    </para>

    <para style='BodyText'>
        ...a library for creating presentation slides.
    </para>
</frame>

Webserving with Python

search for script_server.py

Cheetah - The Python Powered Template Engine

Example Template (simple.tmpl)

<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
<hr/>
$content

<hr/>
<p>
<center>$backlink</center><br/>
<center>$copyright</center><br/>

</p>
</body>
</html>

HTML generation script (fills in template)

def gen_beer_can_chicken():
    beer_can_chicken = Template(file="simple.tmpl")
    beer_can_chicken.filename = "./current/food/beer_can_chicken.html"

    beer_can_chicken.copyright = ""
    beer_can_chicken.title = "Beer Can Chicken"
    beer_can_chicken.backlink = '<a href="../index.html">Main Index</a>'
    beer_can_chicken.content = """
<h2>Ingredients:</h2>

<ul>
<li>2 whole chickens</li>
<li>1 tbsp. paprika</li>

<li>2 tsp. chili powder</li>
<li>1 tsp. oregano</li>

<li>1 tsp. salt</li>
<li>1 tsp. black pepper</li>
<li>1/4 tsp. cayenne pepper</li>
<li>1/2 tsp. garlic powder</li>
<li>1 tbsp. brown sugar</li>

<li>2 1/2 full cans of your favorite beer</li>
<li>1 onion diced</li>

<li>2 cloves garlic diced</li>
</ul>
<hr>

<h2>Directions:</h2>
<p>
Trim chickens of excess fat.  Rinse inside and out.  Pat dry.<br>
</p>
<p>
Combine paprika, chili powder, oregano, salt, black pepper, cayenne pepper, <br>
garlic powder, and brown sugar in a small bowl.  Mix well and rub <br>

over the inside and outside of the chickens. <br>

</p>
<p>
Open beers and drink half of each can.  Stuff chopped onions and <br>
garlic into the beer cans.  Place the chicken on the beer can, feet down. <br>
While cooking support the chicken using beer can and the feet.  Be carefull <br>

not to let the chickens fall over on the grill.  Grill over charcoal for about <br>
2 hours.  Add charcoal as necessary. <br>
</p>
<p>
Serves 2.<br>
</p>
"""
    print beer_can_chicken
    fh_beer_can_chicken = open(beer_can_chicken.filename, 'w')
    fh_beer_can_chicken.write("%s" % beer_can_chicken)
    fh_beer_can_chicken.close()
    return

MoinMoin Wiki Engine

MoinMoin is a collaborative Wiki engine written in Python.

MoinMoin is available here:

There is also a desktop edition (MMDE) available for personal desktop use. The desktop edition contains a search engine, revisioning, file uploads, and PNG based drawing support.

The desktop edition is available here:

After installation, point your browser to localhost:8080. The Python based webserver only responds to requests from localhost by default.

Supports python code coloration.


Zope

Zope is an open source application server for building content management systems, intranets, portals, and custom applications.

Zope Management Interface Screenshot

zope screenshot

Plone

Plone is a user-friendly powerful content management system - ideal as an intranet and extranet server, as a document publishing system, a portal server and as a groupware tool for collaboration between separately located entities.

Plone Screenshot

plone screenshot

Django Web Framework


Twisted Network Framework

Twisted is an event-driven networking framework written in Python and licensed under the MIT license.


More Python Framworks


Python Testing