# This lab session is to get them used to the editor, running python scripts # and using python as a calculator. Since I want them to get used to the # editor, they should be discouraged from using the python interpreter, even # though that is easier to use. # Make all of them open an editor. Give them some problem whose solution # just uses formulas involving +, -, * , / and ** (raising to the power). # Here are some examples. # 1. Two trains T and R are moving on the x-axis with velocities 12 m/s and # 11.5 m/s respectively. Suppose at time t = 0 suppose they are at the # points 0 and 1 respectively. At what time, will they collide? Where will # the collide? u = 12.0 v = 11.5 # pos_T_at_t = 0 + u * t # pos_R_at_t = 1 + v * t # We want to solve pos_T_at_t - pos_R_at_t = 0 for t. That is, -1 + (u - v) # * t = 0. Therefore, t = 1 / (u - v) t = 1 / (u - v) pos = u * t print "Prob 1 : The trains will collide in %g at time %g." % (pos, t) # 2. Formula for compound interest. Suppose the principal is P, the interest # rate be r/100 and the number of years by n. Then the total amount of n # years is P(1 + r/100)^n. P = 100 r = 5.0 n = 5 C = P * ( 1 + r / 100)**n print "Prob 2 : Total sum after 5 years is %g." % C # 3. Find the sum 1 + 2 + ... + 100 using the formula 1 + .. + n = n (n+1) # /2 n = 100 S = n * (n + 1) / 2.0 print "Prob 3 : 1 + ... + 100 = %d." % S # 4. Write all the above in an editor, save it as a .py file and email it to # themselves?