For Loops and Tuples

The for statement iterates over the elements of a sequence

     for i in [3,4,5,6,7]:
         print i 
  • In this case, i is assigned to 3, 4, 5, ...

If the sequence contains tuples of equal size, can assign to multiple names

     for x,y in [(2,3),(4,5),(5,6),(7,8)]:
          plot(x,y) 
  • In this case, the loop executes plot(2,3), plot(4,5), plot(5,6), etc...
  • Again, this only works if all elements are tuples of same size.
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 67
July 17, 2000, beazley@cs.uchicago.edu
>>>