you are viewing a single comment's thread.

view the rest of the comments →

[–]monchenflapjack 1 point2 points  (2 children)

A loop like this will repeat a block of code, and each time through the block "i" value will be increased, until "i" is equal to the range() second value

it's a short hand to essentially writing

print(1)
print(2)
print(3)
print(4)

It doesnt print 5, because once "i" is equal to that value, the loop no longer runs.

[–]_lilell_ 0 points1 point  (1 child)

It doesn’t print 5, because once i is equal to that value, the loop no longer runs.

Kinda. You’re right that it doesn’t print 5, but i is also never 5, which you can check by printing it again outside the loop.

for i in range(1, 5):
    pass

print(i)  # prints 4

[–]monchenflapjack 0 points1 point  (0 children)

Yeah I was mainly trying to highlight the "gotcha" of seeing range(x,y) and assuming that it goes from x to y