Compact summary of python This is not intended to be the complete language. name your file xxx.py execute your file python xxx.py on MS Windows only pythonw xxx.py when using Tk, wx, Qt execute your file and save output in a file python xxx.py > xxx.out # rest of line is a comment, # sign makes the rest of a line a comment """ starts a long comment, can be many lines """ # ends the long comment Indentation must remain the same on a sequence of statements. (statements starting next indentation, end with a colon : ) Indent after each compound statement such as if, while, for, def ... print "hello from python" # quotes make strings, writes to display print 'hello from python' # apostrophes make strings, no character type print "hello", print "from python" # because of previous comma, writes same one line # see files hello.py and hello.out print x # prints the value of x, any type print "x=", print x # prints one line, e.g. x= 7.25e-27 Strings may use either " or ' (same on both ends) Open a file for reading or writing, f is any variable name f=open("myfile.dat", "r") # open for reading f=open("myfile.dat", "w") # open for writing f.close() # close the file a=f.read() # read entire file into string a a=readline() # read one line, including \ n, into string a f.seek(5) # seek five # see files testio.py and testio.out Reserved words: and as assert break class continue def del elif else except exec finally for from global if import in is lambda not or pass print raise return try while with yield Operators: y=7 # the usual assignment statements with normal precedence z=5.3 x=y+z x=-y x+=1 # in place of x++; the list of operators is: + - * ** / // % arithmetic << >> & | ^ ~ shift logical < > <= >= != and or comparison Delimiters include: ( ) [ ] { } @ grouping , : . ` ; separating = += -= *= /= //= %= operate store &= |= ^= >>= <<= **= operate store Special characters: ' " # \ Not used: $ ? Numbers: 1 123456789012345678 1.3 7.5e-200 0xFF 5.9+7.2j Boolean: True False constants Tuples: x=2, 3, 5, 7, 11 y=(2, 3, 5, 7, 11) Lists: x=[2, 3, 5, 7, 11] An array of 10 elements, initialized to zeros: z=[0 for i in range(10)] z[0]=z[9] # lowest and highest subscripts A 2D array of 3 by 4 elements, initialized to zeros: a=[[0 for j in range(4)] for i in range(3)] a[0][0]=a[2][3] # lowest and highest subscripts a[i][j] import math # see files testmath.py and testmath.out a=math.sin(b) print "b=%f, sin(b)=%f" % (b, a) # formatted output %d, %10.5f, %g ... import decimal # big decimal a=decimal.Decimal("39.25e-200") import random a=random.randint(1,10) import NumPy # latest numerics package import Numarray # older numerics package Multiarray ? import Numeric # very old numerics package Three equivalent ways to call QString: from PyQt4.QtCore import * # all directly visible x=QString() import PyQt4.QtCore as P # any short name x=P.QString() import PyQt4 x=PyQt4.QtCore.QString() # fully named You can have your own program in many files. e.g. file spiral_trough.py has a function spiral def spiral(x,y) in test_spiral.py from spiral_trough import spiral z=spiral(x1,y1) # control statements zero or null is True # anything else is False if ad: # >= for greater than or equal statements elif e==f: # != for not equal statements else: statements # unindent to terminate for i in range(n): # i=0, 1, 2, ... , n-1 for i in range(3,6): # i=3, 4, 5 not 6 for i in range(5,0,-1): # i=5, 4, 3, 2, 1 not 0 while boolean_expression: # || is "or" && is "and" True, False "break" exits loop or "if" statement. "continue" goes to next iteration in a loop. Function definition and call: def aFunction(x, y, z): # inputs of any type # do something, compute a and b return a,b # somewhere later m, n = aFunction(p, q, r) # none, one, or multiple value return one or several functions defined in a file # funct_to_import.py has two functions defined, funct and rabc def funct(n): return n a,b,c=0,0,0 def rabc(): a=1 b="rabc string" c=[3, 4, 5, 6] return a,b,c In another file, call funct and rabc # test_import_funct.py import funct_to_import and run funct and rabc import funct_to_import print funct_to_import.funct(3) print funct_to_import.rabc() x,y,z = funct_to_import.rabc() print x print y print "z[1]=", print z[1] Define and use a class: # test_class.py class chair(object): """documentation goes here, many lines OK """ def __init__(self, name, legs=4): # 'self' is a convention, may be any name self.name=name # these are the class data structure self.legs=legs def prnt(self): # may have more arguments print "chair.prnt =", print self.name, self.legs def set(self, newname): self.name=newname def get_legs(self): return self.legs def _cost(self): # should not be used outside return "$"+str(self.legs*5) cost=property(fget=_cost) # cost is now like name and legs chair1=chair("small") # create an instance of a class chair1.prnt() # call a method of the instance chair2=chair("large",6) # create another instance chair2.prnt() chair2.set("large_butt") chair2.prnt() print "legs=", print chair2.get_legs() print chair2.name, chair2.legs, chair2.cost Output is: test_class_py.out: chair.prnt = small 4 chair.prnt = large 6 chair.prnt = large_butt 6 legs= 6 large_butt 6 $30 To find where packages are stored on your computer: # package_test.py import sys import user print 'sys.path' # search list for "import" packages print sys.path print ' ' print 'dir(user)' print dir(user) print ' ' print 'user.pythonrc' # possible to customise your python use print user.pythonrc