Oct-02-2018, 12:05 AM
To elaborate more on the origin of the "month" variable, for loops instantiate a variable when used. So
Likewise, if we iterate over an iterable such as a list:
Because of this behavior, we can use the variable within the loop, as buran did, and it will always use the current assigned value for the variable.
for month in range(1, 13):instantiates the variable "month" and assigns the variable to the number for that loop. In effect it assigns month to 1 for the first iteration of the loop, reassigns it to 2 for the second iteration, etc.
Likewise, if we iterate over an iterable such as a list:
for month in ["Jan", "Feb", "Mar", "Apr", "May", ...]:then it would assign month to "Jan" for the first iteration, reassign to "Feb" for the second iteration, etc.
Because of this behavior, we can use the variable within the loop, as buran did, and it will always use the current assigned value for the variable.