use a list to determine the range of a for loop
>>> x = [0,1,2,3,4] for in x: print i, 1 2 3 4
Loop A String
' python ' for inch x: Print i, p y t h o n
Tuple for Loop
>>> x = [('http','HTTPS'),('Java','python')]>>> for(A, B)inchx:Print(A, b) ('http','HTTPS')('Java','python')
iterators
#file iterators, best practices for reading files>>> forLineinchOpen'Test.txt'): Printline.upper () hello,word!#Dictionary iterators>>> testdict = {'name':'Ethon','Aender':'male'}>>> forKeyinchtestdict:PrintKey +':'+Testdict[key] Aender:malename:ethon
iterative Protocols : Some functions can be run on objects that support an iterative protocol
>>> testlist = [9,8,7,6,5]>>>PrintSorted (testlist) [5, 6, 7, 8, 9]>>>Printsum (testlist)35>>>PrintAny (testlist) True>>>PrintList (Open ('Test.txt'))['hello,word!']>>>PrintTuple (Open ('Test.txt'))('hello,word!',)
#tuples, list constructors, and joins can operate on objects that support iterative protocols>>>Print('--'). Join (Open ('Test.txt')) hello,word!
Use the range function to produce a range of loops
for in range (5): print'was the currentvalue' is The current value is The current value is The current value is The current value is the current value
Zip Zip : Use the zip function to merge two lists into a list of tuples.
>>> L1 = [1,3,5,7]>>> L2 = [2,4,6,8]print zip (l1,l2) [(1, 2), (3, 4 ), (5, 6), (7, 8)]for in Zip (l1,l2): Print (A, b) ( 1, 2)(3, 4)(5, 6) (7, 8)
Python Learning summary 1:for loop control statement