def sep() : sepstr = "-" * 75 print sepstr secno = 0 # Keeps track of the section number. def header(sectitle) : global secno secno += 1 print sep() print "%5d. %s" % (secno, sectitle) sep() print def pause() : raw_input() def bigpause() : print raw_input("press enter to continue") print "\n" * 24 # Tuples print "Press enter at pauses." pause() #---------------------------------------------------------------- header("Tuples") tuple1 = (2, 5.4, "some object") tuple2 = (["a", "list", "of", "words"], ("another", "tuple")) print tuple1 print tuple2 print "Second elemeent of the first tuple1 : ", tuple1[1] print "First element of the second element of tuple2 : ", tuple2[1][0] #----------------------------------------------------------------- pause() # However modifying a tuple is not allowed. Uncomment the following lines # one by one to see for yourself : # # tuple1.append('a') # del tuple2[0] # tuple2[0] = "same" # However you can reassign the whole tuple tuple2 = ("new", "tuple") print "New tuple2 :", tuple2 # You can add two tuples print tuple1 + tuple2 #--------------------------------------------------------------------- pause() # This gives a "hack" to append to tuples. tuple2 = tuple2 + ("appended", "words") print "Appended tuple2 :", tuple2 #------------------------------------------------------------------- # As we saw last time, we can assign tuples: (a, b) = (2, 3) print "Tuple assignment : a =", a, "," print " b =", b, "." # Or you can forget the parenthesis : c, d = 5, 6 print "Second tuple assignemnt : c = %d, d = %d." % (c, d) bigpause() # elif header("elif") # In one of the lab sessions we had a bunch of nested ifs. elif is a more # elegant way of handling that. # Suppose you want to say a number is positive negative or zero, n = -5 # n = input("No : ") if n > 0 : print "n = %d is positive." % n elif n < 0 : print "n = %d is negative." % n else : print "n is zero." #------------------------------------------------------ pause() # Another example ans = "yes" # ans = input("Do you like pears? (valid answers : yes/no/what) : ") if ans == "yes" : print "You should try poached pears." elif ans == "no" : print "Too bad. Do you like apples?" elif ans == "what" : print "Google pear." else : print "Invalid answer." bigpause() # functions header("functions") # Function to compute nth power of a float. def my_power(x, n) : x = float(x) if n == 0 : pow = 1 elif n > 0 : pow = 1 for i in range(n) : pow *= x elif n < 0 : pow = 1.0 for i in range(-n) : pow /= x else : print "Really?" return pow print "2 ** 3 =", my_power(2, 3) print "2 ** -3 =", my_power(2, -3) print "0 ** -0 =", my_power(0, -0) #---------------------------------------------------------- pause() # Second way to do the same thing. import math def my_power2(x, n) : if n == 0 : pow = 1 elif x == 0 and n != 0 : pow = 0 else : pow = math.exp(n * math.log(x)) return pow print "2 ** 3 =", my_power2(2, 3) print "2 ** -3 =", my_power2(2, -3) print "0 ** -0 =", my_power2(0, -0) pause() # computing mean and standard deviation lst_data = [100.0, 50.5, 23.5, 23.4, 9.4, 434.5] def mean(list_of_nos) : sum = 0 for number in list_of_nos : sum += number mean = float(sum) / len(list_of_nos) return mean print "Mean =", mean(lst_data) def sd(list_of_nos) : sumdev = 0.0 m = mean(list_of_nos) for no in list_of_nos : sumdev += (no - m) * (no - m) stdev = math.sqrt(sumdev / len(list_of_nos)) return stdev print "s.d. =", sd(lst_data) # Another formula for s.d. def sd2(list_of_nos) : sumsq = 0 sumval = 0 for no in list_of_nos : sumval += no sumsq += no * no stdevsq = (sumsq - 1.0/len(list_of_nos) * sumval * sumval)/len(list_of_nos) stdev = math.sqrt(stdevsq) return stdev print "s.d. by the second function=", sd2(lst_data)