# 1. del and index # 2. len of a list # 3. while loop : making a Celsius to Fahrenheit chart. # 4. computing sin(x) = x - x^3/3! + x^5/5! ... # 5. for loops and finishing the ctof program # 6. Some more pretty printing print "---------------------------" print "Some more more lists" print "---------------------------" # 1. del and index # Suppose you have a list my_list = ['apple', 'banana', 'guava', 'chikoo'] print "New print format : my_list =", my_list print "Prev print format : my_list = %s" % my_list # Deleting guava. Finding guava in the list. n = my_list.index('guava') print "Guava is in position", n, "." # What happens when you search for something not in the list. The following # two lines give ValueError # n1 = my_list.index('papaya') # print "Papaya is in position", n1, "." # One can try if 'papaya' in my_list : n1 = my_list.index('papaya') print "Papaya is in position", n1 else : print "Papaya is not in the list." # To delete guava, you can just say del my_list[my_list.index('guava')] print "The list without guava is", my_list # To delete papaya, which does not exist will cause errors; as you can see # by running the following two lines # del my_list[my_list.index('papaya')] # print "The list without papaya is", my_list if "papaya" in my_list : del my_list[my_list.index('papaya')] print "After deleting papaya the list is :", my_list # 2. len of a list # The len function returns the length of the list print "The number of fruits in the list is", len(my_list) my_list.append('pineapple') print "After adding pineapple, the number of fruits is", len(my_list), print "and the fruits are", my_list, "." # 3. while loop : making a Celsius to Fahrenheit chart. # Back to loops. We shall do two problems, making a chart, and summing a # series. # Celsius to Fahrenheit conversion chart print "C F" C = 0; F = 9.0/5 * C + 32; print C, F C = 10; F = 9.0/5 * C + 32; print C, F C = 20; F = 9.0/5 * C + 32; print C, F C = 30; F = 9.0/5 * C + 32; print C, F # This is boring. To facilitate repetitive statements we can use while using # while statement # Set starting point C = -40 # Set increment to be dC = 2 # Set C_max to be 60 # while C <= C_max do the following # F = formula for fahrenheit in terms of C # print C and F # increase C by dC # Once the loop completes halt the program C = -40 dC = 2 C_max = 60 print "C\tF" while C <= C_max : F = 9.0/5 * C + 32 print C, "\t", F C = C + dC # Exercise : Just to get your ideas clear, write a program to make a chart # of Fahrenheit to Celsius conversion. # Experiment with Boolean expressions. For an expression try running # the following code to get an idea of what goes in the if and while # statements. # if : # print "True" # else : # print "False" # 4. computing sin(x) = x - x^3/3! + x^5/5! ... # Let us use the while loop to do something more useful. The above sum is an # infinite series. We cannot sum infinitely many summands on a computer. We # however can sum a large number of summands. Let us denote the number of # summands we want to consider by N. import math # for the factorial funcion k = 1 # k keeps track of which term we are adding. We start with the 1st # term. Note k varies over odd numbers. N = 21 # Change it as you wish. Large values give more accurate results # but take more computer time. x = input("x : ") x = float(x) sign = 1 sinx = 0 # Name of the variable is sinx while k <= N : sinx += sign * x**k / math.factorial(k) # stands for sinx = sinx + RHS sign = -sign k += 2 # similarly stands for k = k + 2 print "sin(%g) = %g. math libray gives the value %g." % (x, sinx, math.sin(x)) # 5. for loops and finishing the ctof program # First create a list containing all the Celsius values we need to convert. C_list = [] C_val = -40 C_incr = 2 C_max = 60 while C_val <= C_max : C_list.append(C_val) C_val += 2 print "The populated C_list is", C_list print "23 in C_list : ", (23 in C_list) print "24 in C_list : ", (24 in C_list) print "C\tF" for C in C_list : F = 9.0/5 * C + 32 print C,"\t",F # 6. Some more pretty printing # Instead of the above, try the following : for C in C_list : F = 9.0/5 * C + 32 print "%5.1f C = %5.1f F" % (C, F)