# Contents # 1. in : some more comments # 2. for loops; implementing for loops using while # 3. for loops: playing with list indices # 4. Changing the list in the for loop # 5. enumerate # 7. string formatting : pg 12 # Don't worry about the following two functions right now: head_count = 0 def header(hstr) : global head_count head_count += 1 ln = "" for i in range(75) : ln += '-' print ln print "%02d.\t%s" % (head_count, hstr) print ln print " " def pause() : raw_input("To continue, press enter ... ") # 1. in : some more comments header("The builtin command : in") my_lst = [5, 7, 9] print "4 in my_lst :", (4 in my_lst) print "7 in my_lst :", (7 in my_lst) print "a in \"bad\" :", ('a' in "bad") print "o in \"bad\" :", ('o' in "bad") print " o in o :", ('o' in 'o') print " a in o :", ('a' in 'o') # Check what error this gives : # print "4 in 4 : ", (4 in 4) print " 4 in [4] :", (4 in [4]) pause() # 2. for loops; implementing for loops using while header("for loops: glorified while loops") somelist = ["Entry 1", "Entry 2", "Entry 3", "Entry 4", "Entry 5", "Entry 6"] for item in somelist : print "Processing \"" + item + "\"." print print """This can be implemented as a while loop as follows : """ i = 0 while i < len(somelist) : print "Processing \"" + somelist[i] + "\"." i += 1 pause() # 3. for loops: playing with list indices header("range, what for?") print "range(10) : ", range(10) # This we know. print "range(-10, 10) : ", range(-10, 10) print "range(-10, 10, 2) : ", range(-10, 10, 2) print "To print fractions we need a for loop" pt5increments = [] for i in range(20) : pt5increments.append(0.5 * i) print pt5increments print pause() # Suppose you want increments by 1.5 for some reason. You can also do that print "For general increments : eg incr = 1.5 : " gen_incr = [] incr = 1.5 # in this code we are assuming increment has at most 2 dec places start = 2 end = 13 # we do the following so that end gets included in the list if round((float(end) - start)/incr) == (float(end) - start)/incr : end = end + incr new_start = 100 * start new_end = 100 * end new_incr = int(100 * incr) for i in range(new_start, new_end, new_incr) : gen_incr.append(1.0 * i / 100) print gen_incr print pause() # A short CtoF function # Suppose you want to convert temp from 25 - 40 C to F with jumps of 2.5 C incr = 2.5 # you can use input if you want start = 25 end = 40 # as usual, fix the issue at the "end" if round((float(end) - start)/incr) == (float(end) - start)/incr : end += incr # Now, make a list as before for temph in range((100 * start), int(100 * end), int(100 * incr)) : C_temp = float(temph)/100 F_temp = 9.0 / 5 * C_temp + 32 print "%010.3f C = %010.3f F" % (C_temp, F_temp) pause() print # 4. Changing the list in the for loop # Suppose you want to print a log table for integers from 1 to n header("Changing the list in the for loop; enumerate") import math log_of_nos_to_10 = range(1, 11) print log_of_nos_to_10 for no in log_of_nos_to_10 : no = math.log(no) print log_of_nos_to_10 pause() print print "enumerate : ", enumerate(log_of_nos_to_10) print """However "in" works when used as follows. ((0, 1) in enumerate(log_of_nos_to_10)) : """, ((0, 1) in \ enumerate(log_of_nos_to_10)), """ ((2, 2) in enumerate(log_of_nos_to_10)) : """, ((2, 2) in \ enumerate(log_of_nos_to_10)) pause() print for (i, no) in enumerate(log_of_nos_to_10) : log_of_nos_to_10[i] = (math.log(no), math.log10(no)) print log_of_nos_to_10