Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
more looping help?
#5
To elaborate more on the origin of the "month" variable, for loops instantiate a variable when used. So

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.
Reply


Messages In This Thread
more looping help? - by pcsailor - Oct-01-2018, 04:32 AM
RE: more looping help? - by buran - Oct-01-2018, 06:45 AM
RE: more looping help? - by pcsailor - Oct-01-2018, 09:15 AM
RE: more looping help? - by buran - Oct-01-2018, 10:20 AM
RE: more looping help? - by stullis - Oct-02-2018, 12:05 AM
RE: more looping help? - by pcsailor - Oct-03-2018, 05:05 AM
RE: more looping help? - by buran - Oct-03-2018, 06:04 AM
RE: more looping help? - by wavic - Oct-03-2018, 06:22 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020