# Overview # function definitions # tuple and array basics # more on while loops x = 2 def f1(price,rate): tax = price*rate return tax # f1 #print f1(19.99,0.0875) #print f1(9.99, 0.05) x = 0 while x<10: print x x = x+1 # x += 1 # end while print x print "---------" # Fibonacci sequence: 1 1 2 3 5 8 13 21 34 ... a = 1 b = 1 counter = 0 while b < 2**20: print b, " ", (a,b) = (b,a+b) counter += 1 # while print "counter is now ", counter # 5! = 5*4*3*2*1 # 0! = 1 def factorial(n): # compute n! # if n<0: exit("bad parameter") ax = 1 # accumlates running product while n>1: ax = ax * n n = n-1 # n-=1 # while return ax # factorial print factorial(5) print factorial(0) #print factorial(-2) # test if a number is a prime number: 2 3 5 7 11 13 17 ... def isprime(n): if n==2: return True else: k = 2 #.... answer = True while (k<= n/2 and answer==True): if n%k == 0: answer = False k = k+1 #while #else return answer # isprime print isprime(10) print isprime(2) print isprime(19)