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

16

u/Fermter Almost OK Oct 09 '20

OK, this is more a function of what "range" is doing that what the "for" loop is doing.

Here, the for"loop is just setting v to be every value returned by range(3, 10, 3), and then printing that value.

Range, on the other hand, is doing something special. The 3 values you're passing to range are, respectively, the start, stop, and step. Range starts at the start, stops before the stop, and increments by the step every time.

In this case, the start is 3, the stop is 10, and the step is 3. Therefore, the range returns 3, 6, and 9, in that order (because it starts at 3, 3+3 = 6, 6+3 = 9, and 9+3 = 12, which is bigger than 10, so it stops and doesn't do anything with that value).

From the perspective of the for loop, v is set to 3 first, then printed in print(v). Then, it is set to 6 and printed, and then set to 9 and printed.

4

u/[deleted] Oct 09 '20

Oh my goodness this makes perfect sense.

I did not understand what range was fully doing.

4

u/stormsnake Oct 09 '20

You can take a look in the python interactive interpreter, although in this case there's a catch:

>>> range(3, 10, 3)
range(3, 10, 3)  <-- this is an annoying representation of the return value of the range function

>>> list(range(3, 10, 3))  <-- here's how to preview a range
[3, 6, 9]

4

u/Swipecat Oct 09 '20

Yeah, it totally does have to be shown in that "annoying" representation, though, because the whole point of "range" in Python 3 is that it does not expand into a list the way it did in Python 2. Instead, it "generates" the values on-the-fly as and when they are requested by the "for" mechanism. It doesn't waste memory space that way.

Putting "list" up-front does read "range" into a fully expanded list until "range" is exhausted.

1

u/drago3871 Oct 09 '20

Why it is an annoying representation for you? If there is large sequence, what would you want to see as an output?)

2

u/stormsnake Oct 11 '20

I think it's not clear to beginners. Something like "<iterable range from 3 to 10 by 3>" might be better (I ran range and got some iterable object).

I see the point of having the repr be valid python where possible, which is why a repr of "range(3,10,3)" is only annoying here, and not broken :)

1

u/TouchingTheVodka Who's hiring? Jr-Mid Remote/UK - PM me! Oct 09 '20

range is not a function, range is a class. The output is correct.

2

u/scrdest Oct 09 '20

That's a distinction without a difference in Python - all functions are subclasses, either of the 'function' class, or any other class which also happens to define __call__().

3

u/TouchingTheVodka Who's hiring? Jr-Mid Remote/UK - PM me! Oct 09 '20

No, there is a very real difference - when you call range the thing you're getting back is an object of type range. range objects have several useful properties such as a __contains__ method backed by arithmetic rather than iteration, allowing for constant-time membership checking.

>>> r = range(1, 2, 3)
>>> type(r)
<class 'range'>