Vous êtes sur la page 1sur 6

Python Overview

Overview:

Python is an interpreted language.


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

Books:

Python in a Nutshell (O'Reilly).

The Python Cookbook.

Strengths:

Quick to write, no compilation

Fully object-oriented.

Many reliable libraries.

Online Resources:

Weaknesses:

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.

Main
documentation
and
http://www.python.org/doc

tutorial

on

Tutorial for experienced programmers:


http://www.diveintopython.org
Tutorial for beginners:
http://greenteapress.com/thinkpython/thinkCSpy/html/
Python Reference Manual lookup of language
basics.
http://docs.python.org/lib/lib.html
Global Module Index description of all standard
modules:
http://docs.python.org/modindex.html

by Kristian Rother

Development Tools
IDEs

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

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

by Kristian Rother

The Python Command Line (CLI)


Overview:

You can use any Python command from the


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

Tips:

Everything works in exactly the same way


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

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

0
1
2
>>>

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


Essentials:

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
purposes.

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:

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

The command 'b' displays all breakpoints set.

import pdb
pdb.set_trace()

The debugger shows a shell that works like


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

Tips:

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


verb.
Don't re-invent the wheel, using libraries
makes your code shorter and easier to
maintain.
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