from numpy import * # if statments are fairly straightforward if True: print "True" if False: print "False" a = 2 if a == 2: print "a equals 2" else: print "a does not equal 2" # different conditions are combined via "and" and "or" b = 5 if a == 2 and b > 4: print "a equals 2 and b is greater than 4" elif b > 3: print "at least b is greater 3" # for loops start with general structure "for element in list:" for elem in [1,2,3,4,8]: print elem # in many for j in xrange(10): print j print 80*'-' # this works e.g. with arrays x = random.randn(10) for rv in x: print rv print 80*'-' # for loops also take iterators which can be thought of as lists for i,rx in enumerate(x): print i print x print 80*'-' y = random.randn(10) for xy in zip(x,y): print xy print 80*'-' f = lambda z: z**2 + 3. print [f(elem) for elem in x if elem < 1] def quicksort(x): return x if len(x)<=1 else quicksort([e for e in x if e < x[0]]) + [x[0]] + quicksort([e for e in x if e > x[0]])