r/learnpython • u/[deleted] • Oct 09 '20
Understanding the For Loop
Hey All,
I'm having the hardest time understanding for loops in Python.
Could someone explain what is happening in this example? I see we have a variable v. I somewhat understand that "range" is built into Python. I don't understand the output.
for v in range(3, 10, 3):
print(v)Output:
3
6
9
I keep seeing examples online but they lose me every time. Thank you in advance.
-------------------------------------------------
*Edit Thanks to everyone who replied. My misunderstanding was not knowing what range was fully doing.
range(start, stop, step)
366
Upvotes
1
u/greebo42 Oct 09 '20
I had written programs in C several years ago, went a long time without programming, and then started to pick up Python, and the for loop puzzled me at first.
Now I understand that what it's doing is iterating over some set of things which are arranged in a defined order. That set of things could be a list of strings. That set of things could be a list of integers. Or whatever (I'm not talking about the python "set").
So, the range function generates a set of things in a defined order. The example shown is pretty much like the way C does it. The neat thing is that for can be used in many other ways, so you are not /limited/ to the way C does it.