Professional Documents
Culture Documents
Keeping Track of Enumeration Number: Python Scientific Lecture Notes, Release 2012.3 (Euroscipy 2012)
Keeping Track of Enumeration Number: Python Scientific Lecture Notes, Release 2012.3 (Euroscipy 2012)
3 (EuroScipy 2012)
Warning: Not safe to modify the sequence you are iterating over.
Common task is to iterate over a sequence while keeping track of the item number.
• Could use while loop with a counter as above. Or a for loop:
>>> for i in range(0, len(words)):
... print(i, words[i])
0 cool
1 powerful
2 readable
Use iteritems:
>>> d = {’a’: 1, ’b’:1.2, ’c’:1j}
Exercise
Compute the decimals of Pi using the Wallis formula:
∞
Y 4i2
π=2
i=1
4i2 − 1