4.2. Esempi

fibonacci.py

#This program calulates the fibonnaci sequence
a = 0
b = 1
count = 0
max_count = 20
while count < max_count:
    count = count + 1
    #we need to keep track of a since we change it
    old_a = a
    a = b
    b = old_a + b
    #Notice that the , at the end of a print statement keeps it
    # from switching to a new line
    print old_a,
print
Output:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
password.py
# Waits until a password has been entered.  Use control-C to break out with out
# the password

password = "foobar"

#note that != means not equal
while password != "unicorn":
    password = raw_input("Password:")
print "Welcome in"
Esecuzione:
Password:auo
Password:y22
Password:password
Password:open sesame
Password:unicorn
Welcome in