What is the 'animal' variable in for loop?


Hello all I am currently learning python as my first programming language. I am on lesson More with ‘for’ and saw in the example the following code…

animals = [“cat”, “ant”, “bat”]

for animal in animals:
print animal

What I am having trouble understanding is how it is able to recognize the ‘animal’ part of the for loop, when the variable has not been defined. At first I thought perhaps it just recognizes this as the singular form of animals, but I doubt that makes sense. Am I to understand that the variable animal is being assigned to each of the animals in the list automatically and that the “for animal in animals” loop would work just as well if I were to change the code to be “for blahblahblah in animals” print blahblahblah?

(I did try this and it seemed to confirm my theory but thought I’d ask regardless in case I am missing something)


its defined, you define it in the for loop. Now python will assign all values the list to this variable in order, after assigning the loop iterator, its makes the loop iteration.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.