Vous êtes sur la page 1sur 6

Python Overview


Python is an interpreted language.

The Python interpreter automatically
generates bytecode (.pyc files).


Python in a Nutshell (O'Reilly).

The Python Cookbook.


Quick to write, no compilation

Fully object-oriented.

Many reliable libraries.

Online Resources:


All-round language.

Writing very fast programs is not easy.

The lead developer (Guido van Rossum)

works for Google Inc.
It is 100% free software.




Tutorial for experienced programmers:

Tutorial for beginners:
Python Reference Manual lookup of language
Global Module Index description of all standard

by Kristian Rother

Development Tools

Keyboard shortcuts in IDLE:

Ctrl+] indent a block of code.

Ctrl+[ dedent a block of code.

Alt+3 comment out a block of code.

Erik Powerful development environment that helps

managing big projects with many files, as well as
testing and debugging them.

Alt+4 uncomment a block of code.

F5 run a script.

Eclipse A Python plug-in for Eclipse exists as well.

Alt+X check a script for syntax errors.

IDLEStandard Python editor that comes with Python.

Good for most small programs.
SPE To date, the most advanced non-commercial
development environment

Creates HTML pages from the comments
in a bunch of Python modules.
PyChecker Checks Python source code for common
Tool for the Python command line
that supports Tab-expansion.
A Python compiler that creates Java code.
Useful to make Java and Python programs work
together, and to run Python programs from a web

by Kristian Rother

The Python Command Line (CLI)


You can use any Python command from the

interactive Python command line:
>>> print 4**2
>>> a = 'blue'
>>> print a


Everything works in exactly the same way

as it would in a program.
You can define code blocks by writing extra

The CLI works great as a pocket calculator.

Writing code blocks with 2+ lines in the CLI gets
painful quickly.
From IDLE, you can execute a program in the
command line by pressing F5.

More Info:

>>> for i in range(3):

print i


You can leave the command line by Ctrl-Z

(Windows) or Ctrl-D (Linux).

An enhanced CLI with syntax highlighting and tab

expansion is provided by the Ipython package
(Windows/Linux/Mac) http://ipython.scipy.org.

by Kristian Rother

Writing Python Programs


All program files should have the

extension .py

Commenting code:

When developing on Unix, the first line in

each Python program should be:

# this is a comment.
print a + b # adding the variables.

#!/usr/bin env python

In Python, single lines can be commented by the

hash symbol:

Indentation is a central element of Python

syntax, marking code blocks. Code blocks
should be indented by four spaces/one tab.
Indentation must not be used for decorative

Also, multi-line comments can be enclosed by

triple quotes:

This is some longer descriptions

that stretches over multiple lines.

Only one command per line is allowed.

The triple quoted comments can be used to

generate documentation automatically.

What Python does not have:

Memory allocation.

Declaration of variables and types.

Strict object-orientation.

Line numbers.

by Kristian Rother

Using the Python Debugger (pdb)

Interrupting program execution:

You can interrupt the program execution and

start the debugger by inserting these
commands at any point:

The command 'b <line number>' sets a breakpoint
at the given line.

The command 'b' displays all breakpoints set.

import pdb

The debugger shows a shell that works like

the normal Python command line, but with
some extra commands:


n (next) execute next statement.

s (step) execute next statement, and
descend into functions.

>>> a = 1

l (list) show source code.

c (continue) continue execution until
the next breakpoint.

help print help message.

q (quit) abort the program.

A valuable diagnostic tool is to use the debugger

for manipulate variable values. e.g.:

Most IDE's are supporting the debugger. It is

much more comfortable to control the program
from there.
In the debugger command line you can get help
on any command by typing 'help <command>'.

by Kristian Rother

Writing good code

Coding guidelines:

The Dogma of Programming

Divide the program into functions

First, make it work.

Write short docstrings for all functions.

Second, make it nice.

Use reasonable variable names.

Third, and only if it is really neccessary,

make it fast.

Use reasonable function names starting with a

Don't re-invent the wheel, using libraries
makes your code shorter and easier to
Writing test functions saves lots of time on a
long term.

Code formatting conventions:

Use spaces instead of tabs (or use an editor that

converts them automatically.

Keep lines less than 80 characters long.

Separate functions with two blank lines.

Separate logical chunks of long functions with a

single blank line.
Variables and function names in lowercase.

by Kristian Rother