#!/usr/bin/env python # coding: utf-8 # # D - 'for' loops # # ![lec-0D-ga](lec-0D-ga.png) # # * To try it on *python tutor live*, [click here][preloaded ptl D] # # [preloaded ptl D]: http://www.pythontutor.com/live.html#code=%23%20%3D%3D%3D%20Lecture%20zero,%20part%20D%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%23%20---%20Remember%20from%20part%20C%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Amain_R%20%3D%208.314%0Adef%20function_V(dummy_T,dummy_P%29%3A%0A%09return%20main_R%20*%20dummy_T%20/%20dummy_P%0Amain_P%3D1e5%0Alist_T%20%3D%20%5B273,%20298,%20373%5D%0A%23%20---%20for%20loop%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Afor%20i%20in%20range(3%29%3A%20%23%20the%20function%20range%203%20says%20i%20should%20loop%203%20times%20with%20index%20starting%20from%20zero%20and%20increasing%20in%201%20after%20each%20iteration%0A%09print(function_V(dummy_T%3Dlist_T%5Bi%5D,dummy_P%3Dmain_P%29%29&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-live.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false # In[1]: # The following code should be studied on the platform provided by http://www.pythontutor.com/live.html#mode=edit # This platform graphically depicts the behavior of variables, assignments, functions and pointers, # which is the major source of confusion among beginners in programming # === Lecture zero, part D ======================================================================================================================================================================== # --- Remember from part C ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- main_R = 8.314 def function_V(dummy_T,dummy_P): return main_R * dummy_T / dummy_P main_P=1e5 list_T = [273, 298, 373] # --- for loop ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- for i in range(3): # the function range 3 says i should loop 3 times with index starting from zero and increasing in 1 after each iteration print(function_V(dummy_T=list_T[i],dummy_P=main_P)) # we might want to keep track of the results in each iteration and take decisions -- for that see "if" syntax in Lecture zero, part E