#! /usr/bin/python a,b = 0,1 while a < 1000: print a a,b = b,a+b print b def fib(n): a,b = 0,1 while a < n: print a a,b = b,a+b return b # if omitted, return the value 'None' print fib(100) print b frog1 = (50,100,"up") # pattern matching for de-structing tuples: (x,y,vel) = frog1 WORLD_WIDTH = 500 import random # can now use random.randint(), etc. class Princess: WAIT_TIME = 10 def update(self): self.timeLeft = self.timeLeft - 1 if self.timeLeft == 0: self.timeLeft = Princess.WAIT_TIME; self.x = random.randrange(WORLD_WIDTH) # A constructor: # def __init__(self, _x): self.x = _x self.timeLeft = Princess.WAIT_TIME # Override the 'str' function: # def __str__(self): return 'Princess:' + str(self.__dict__) p = Princess(50) print str(p) # could also just say `print p` p.update() print str(p) i = 0 while i < Princess.WAIT_TIME + 2: p.update() i = i + 1 print Princess.WAIT_TIME+2, "units later", p