r/learnpython • • 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

62 comments sorted by

View all comments

1

u/TheDyslexicDemon Oct 09 '20

Uh I’m also new to programming but here’s what I got: When you’re printing v you’re printing the range specified (range(3, 10, 3)); the first number being where the program will start, the last number being by how much (like counting by 2s: 2 4 6 8), and the middle number(10) is the most the program can go up to. Remember that the middle number is non inclusive (so the most is actually the middle number - 1).