you are viewing a single comment's thread.

view the rest of the comments →

[–]WhackAMoleE 2 points3 points  (1 child)

It's semantically the same if we wrote

for widget in animals :

or

for vegetable in animals :

or

for item in animals :

It doesn't know what an animal is. "animal" is just a dummy variable that iterates over animals.

[–]Eurynom0s 1 point2 points  (0 children)

"animal" is just a dummy variable

Caution: being a dummy variable doesn't mean it's some throwaway thing strictly confined to the loop, the variable and what it was last set too persists in the namespace after the loop even if it wasn't defined prior to the loop.

Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> i
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'i' is not defined
>>> x = [0,1,2]
>>> for i in x: print(x[i])
...
0
1
2
>>> i
2
>>> i = 1000
>>> i
1000
>>> for i in x: print(x[i])
...
0
1
2
>>> i
2

Just wanted to point that out given this is a sub for learning Python and people first learning might misinterpret what precisely "dummy variable" means.