#!/usr/bin/python print "hello world"
#!/usr/bin/python title = "hello world"
#!/usr/bin/python import myfile print myfile.title
#!/usr/bin/python from myfile import title print title


#!/usr/bin/python print "hi " * 4
hi hi hi hi
$ python Python 1.5.2 >>> import string >>> dir(string) [’capitalize’, ’capwords’, ’center’, ’count’, \ ’digits’, ’expandtabs’, ’find’, ’hexdigits’, \ ’index’, ’index_error’, ’join’, ’joinfields’, \ ’letters’, ’ljust’, ’lower’, ’lowercase’, \ ’lstrip’, ’maketrans’, ’octdigits’, ’replace’, \ ’rfind’, ’rindex’, ’rjust’, ’rstrip’, ’split’, \ ’splitfields’, ’strip’, ’swapcase’, \ ’upper’, ’uppercase’, ’whitespace’, ’zfill’]
#!/usr/bin/python
for n in range(1, 13):
print n, "x 8 =", n*8
$ python eight.py 1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 9 x 8 = 72 10 x 8 = 80 11 x 8 = 88 12 x 8 = 96
#!/usr/bin/python
for n in range(2, 10):
print ’n is’, n
else:
print ’finished for loop, n is’, n
$ ./py7.py n is 2 n is 3 n is 4 n is 5 n is 6 n is 7 n is 8 n is 9 finished for loop, n is 9
#!/usr/bin/python
for n in range(2, 10):
print ’n is’, n, ’, range(2, n) is’, range(2, n)
for x in range (2, n):
print ’x is’, x, ’, n % x == ’, n % x
if n % x == 0:
print n, ’equals’, x, ’*’, n/x
break
else:
print n, ’is a prime number’
n is 2 , range(2, n) is [] 2 is a prime number n is 3 , range(2, n) is [2] x is 2 , n % x == 1 3 is a prime number n is 4 , range(2, n) is [2, 3] x is 2 , n % x == 0 4 equals 2 * 2 n is 5 , range(2, n) is [2, 3, 4] x is 2 , n % x == 1 x is 3 , n % x == 2 x is 4 , n % x == 1 5 is a prime number
$ ./py6.py 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3
#!/usr/bin/python
import os.path
from Tkinter import *
def makebutton(message):
w = Button(text=message, command=’exit’)
w.pack()
w.mainloop()
if not os.path.isdir(\
os.path.expandvars(’$HOME/OpenOffice.org1.0.3’)):
makebutton("You might be interested \
in OpenOffice (a word clone) which can be \
invoked from the RedHat menu")
#!/usr/bin/python
import sys
for line in sys.stdin.readlines():
if line[0] == ’#’:
print line
fred:llk:1000:1000:Fred,,,:/home/fred:/bin/bash bob:wei:1001:1001:Bob,,,:/home/bob:/bin/bash alice:kjh:1002:1002:Alice,,,:/home/alice:/bin/bash mary:asd:1003:1003:Mary,,,:/home/mary:/bin/bash
#!/usr/bin/python
import sys, string
for line in open(’/etc/passwd’).readlines():
words = string.split(line, ’:’)
if len(words) >= 5:
print words[4]
Fred Bob Alice Mary
$ man 5 passwd
This document was produced using groff-1.19.