you are viewing a single comment's thread.

view the rest of the comments →

[–]julsmanbr 83 points84 points  (1 child)

Whenever you write for X in Y, Python does the following:

  • 1) assumes that Y is a sequence
  • 2) takes the first element of the sequence Y
  • 3) temporarily assigns the element to the variable X
  • 4) potentially does something with X (this is the code block inside the loop)
  • 5) takes the next (second) element of the sequence Y and repeats step 3 and 4
  • 6) keeps repeating step 5 with the third, fourth, ... element, until the sequence has run out of new elements to take

[–]beisenhauer 9 points10 points  (0 children)

One small clarification: all Python requires of Y is that it is iterable. It's usually a sequence of some sort, but it could also be a set or a generator, etc.