#!/usr/bin/python -B #global import math from go import * root = math.sqrt x=2 @go def _x(): "example of global" print x def fred(y,x=100, z=2,pretty = lambda x: int(x)): "e.g.1) var scope; 2) code as data" print y+z print "x",x print "pretty",pretty(y+z) print root(100) @go def _fred(): fred(20) @go def _fred1(): "example of passing code as data" fred(22.99,pretty = lambda x: x) class yourFirstClass: def __init__(self,x) : self.mine = x def x(self): return self.mine def setX(self,new): self.mine=new @go def _class1(): "example of set, get, init of a class" y = yourFirstClass("morgantown") y.setX("fairmont") y.mine="clarksburg" print y.x() @go def _i1(): "counting" for i in xrange(5,16,3): print i @go def _i2(): "iterating" for x in ["m","f","c"]: print x y ={"a":10,"b":20,"c":30} @go def _dict1(): "accessing a dictionary" print y["c"] @go def _sorteddic(): "example of walking a dictinary in key sort order" for key in sorted(y.keys()): print ":key",key,":val:",y[key] @go def _dict2(): "accessing keys and values" for k,v in y.iteritems(): print ":key new",k,":val:",v def odd(x) : return x % 2 == 1 @go def _odd(): print odd(2), odd(21) def largeOdd(x) : return odd(x) and x > 10 @go def _largeOdd(): return largeOdd(11) @go def lcomp(): "example of list comprehension" print [x/3.0 for x in xrange(60,110,3) if largeOdd(x)] def myOdds() : for x in xrange(60,110,3): if odd(x): yield(x) @go def _iterate(): "example of defining your own iterator" for y in myOdds(): print y if y> 90: break