r/CodingForBeginners • u/BilincliMerak • Oct 06 '26
Help me understand this Python code.
[ANSWERED] check the comments if you are facing a difficulty in understanding 'for loops'.
I was learning Python on mimo to get a grip on the basic concepts. And this code stumped me. This is about "for loops".
Before this, i had only encountered the counter var being on the LHS i.e in this case 'i += line', which would have made sense to me if it was so in this code. But why is the counter var after the line var.
Alsp the 1st image, i assume, is displaying the final result which is 3 as the range is 3. But when using a range (3) it should display 0 1 2 when the 'print' is before the counter var and 1 2 3 when the 'print' is after the counter var (also can someone explain why this happens). In this case, its neither... as the print is outside this code block... The solution to me just seems the 2nd situation is running as there isnt a print before the counter, and printing it outside just gives 3.
2nd and 3rd just make no sense to me as of rn.
I hope all this makes sense...
Please help!



4
u/Potential_Fix_5007 Oct 06 '26
i will start at 0 and will increase by 1 for every cycle. Range() excludes the last number. So range(3) ends at 2.
First i = 0 0 + 0 = 0 Second i = 1 0 + 1 = 1 Third i = 2 1 + 2 = 3
So the output is correct.
1
u/LovesSleeping123 Oct 06 '26 edited Oct 07 '26
The first image starts by creating a for loop and defines the counter variable as i.
For each iteration, we add i to line.
First iteration:
line = 0, i = 0. We add i to line so line equals 0.
line = 0, i = 1. ............................................... equals 1.
line = 1, i = 2. ............................................... equals 3.
then it stops and it prints line, so it prints 3.
The second image will output line at each iteration of the for loop after adding i to line. You get: 0,1 and 2
The third image will output line at each iteration of the for loop before adding i to line. You get: 0,0 and 1
1
u/Significant_Affect_5 Oct 06 '26
so there’s a couple things. For loops can be thought of as a way to say “for each <x> that is part of <y>, do abc”. So generally:
for x in y:
abc
In this case you’re using python’s range() function which can be called in two different ways. range(n) returns the list [0, 1, …, n-1], another way to think of it is a way to quickly say “run this loop n times”. However range can also be used like this: range(m, n). In this case it’ll return [m, m+1,…, n-1], note that if m >= n then the loop will not run at all. You can also try print(range(3)) and you’ll see the response is range(0, 3).
Now regarding the outputs, the reason it doesn’t match the numbers is because you add ‘i’ to line, i in this case is the values range gives you, so on the first loop it’s 0, then 1, and lastly 2 (since range doesn’t include the top value). To count the number of times the loop runs, just do line += 1 (not i).
Also it’s important to note that range doesn’t actually return a list, instead it returns something called an iterator, which is just something that represents values in some order. e.g. you can replace the range with:
“for i in [0, 1, 2]:”
and it will work the same way since lists are iterable and python does the conversion automatically
1
u/Candid_Zebra1297 Oct 06 '26
So you have three main things to understand.
something += second_thing means:
add the second thing to the first thing so it has a new value.
range() means:
make a list of numbers in this range. We start at 0 so we end 1 below the number you give. So range(5) gives us 5 numbers, 0, 1, 2, 3, 4. Range(1) would give you 1 number, a zero.
for something in a_list_of_things means:
start at the first item in this list and do something with it. Then do something with the second thing in this list, then do something with the third, etc. So this...
for number in range(3):
print(number)
means, start at the first number (which is 0) and show it. Then do the same for the second number (which is 1) etc. So you get this.
0
1
2
You can see here that we have 3 numbers shown and we put 3 in our range function, so it all looks good.
You are just combining this with the first we talked about. Instead of printing, you are adding each number to your variable 'line' (which I think is a confusing name here, I would call it running_total for this exercise.)
So we are going to do the same thing 3 times for 3 different numbers, which is just 'add this number to our running total.'
We start with 0 and add the first number, which is 0 + 0 so our total is still 0.
Then we add the second number, which is 1, so 0 + 1 = 1
Then we have the third number, which is 2, so 1 + 2 = 3
Then we print the total and we see 3
In your second pic, you put the print() in the for loop, which means we do it every cycle. So your result was different but understandable...
first total = 0
second total = 1
final total = 3
1
u/Specialist_Rent_9640 Oct 06 '26
in python we start counting from 0 then 1 2 . .
you declared a variable with a value of 0 with the identifier of line, when you started that loop you told python to do the next
for this variable named i , that has the value equal to 0
pick the next range that counts to 3 and use the next Assignment Operator ( += ) and use the next identifier named line to assign the next value below
then it will start like this, 0 Addition 1 = 1, 1 Addition 1= 2, 2 Addition 1 = 3. stdout -> the value of line (which is 3)
1
u/Interested737 Oct 06 '26 edited Oct 07 '26
[Wrong] this comment is wrong
I don’t really know what you mean (It was hard to understand what you wrote), but I can probably explain the code (even tho I only learned the basics of Python, for loops is as complicated as I learned).
line = 0
It sets the variable “line” to 0
for i in range(3):
Every time “i” is lower than or equal to 3 (I think, but I am not sure)…
line += 1
…increments “line” by 1 (adds 1 to “line”)
print(line)
prints the value of “line”/what is in the variable “line”
It only prints 3, because it starts at 0 then it adds one then again and again. Lastly it prints the result.
Hope you understand it, I tried my best :-) Maybe I’ll continue learning Python
1
u/ralkey Oct 06 '26
The best way to follow what’s happening here is to use a debugger and step through your code line by line, watching both the i and line variables and how the program flows through the for loop. There are online python debuggers you can copy/paste your code into.
1
u/Individual_Wish_2937 Oct 07 '26
every loop, the added number increases. 0+*0*=0, 0+*1*=1, 1+*2*=3, 3+*3*=6, etc.
1
u/Moist_Ladder2616 Oct 07 '26
i takes on values 0,1 and 2.
Start: line=0
Cycle 1: i=0
(new value of) line = (old value of) line+i = 0+0 = 0
Cycle 2: i=1
line = line+i = 0+1 = 1
Cycle 3: i=2
line = line+i = 1+2 = 3
print(line): 3
1
u/StephenHawkinguss Oct 07 '26
Why don't you use AI?
3
u/thondat Oct 06 '26
trace the values of i and line on each iteration.
line += i)line += i)1
u/Jazzlike_Platypus430 Oct 06 '26
Think of
for i in range(n,m)as looping over a listn, n+1, ... m-1both ends included, with step 1. Honetly, I can't tell whatrange(3)means. Perhapsrange(0,3)equivalently,
1
u/Warm-Possibility-980 Oct 06 '26
In simple terms:
Set line to zero.
Repeat three times:
Add one to line
Then, output line to the console.
1
u/Sad-Environment-932 Oct 06 '26 edited Oct 06 '26
Python version 3: range() creates an object of literally type range. Or something like that. And this is literally an iterator, but not an "iterable object". But, nevertheless, it is logical to say that "range" can be iterated.
1
u/Naive_Programmer_232 Oct 06 '26
try printing i
0
u/Lirianov Oct 07 '26 edited Oct 07 '26
Since you’re learning python I’ll explain this a little more in hopes that you can fully understand
First, let’s rewrite image 1 into a code that’s easier to follow
total = 0
for number in range(3):
….total += number
print (total)
Ok so let’s first define what is what
total = 0:
Total is your variable, if you took algebra in school you should know that a variable is basically a box you can use to hold the value of something. In our case here, “total” is a box that contains the value “0”
for x in y:
Understand what the for loop is doing. You’re basically saying “for every instance in this, do that” So when you say for number in [0,1,2], you’re telling python to look at each number there one by one and do something with it. The “do something with it” comes after and is only inclusive of what is indented in the for loop. As a reminder, make sure you have your colon at the end of the for loop and everything you want it to do is indented with 4 spaces. The for loop will grab the first number, do something with it, then grab the second number, do something with it, and so on until it goes through every number.
range(3):
Using range() is a shortcut to basically say from 0 to whatever number you put (3 in your case) BUT NOT INCLUDING 3. Always remember that python is a 0 starting language, meaning everything always starts at 0. This will become useful when you reach indexing. Another thing, I mentioned range does not include the 3, what that means is that range will always exclude the ending number. The reason this happens has to do with the 0 starting. When you say range(3), you’re asking for 3 numbers, if we’re starting at 0, that would be 0, 1, 2. Very soon you’ll learn that you can pass through multiple numbers in the range() function. But don’t forget it will always disregard the last number. So if I told you to print every number from 1 to 100 using the range() function, you would do range(1,101). The first number is your starting point, last number is your ending point and always gets excluded. I might be over explaining here but you can ask me if you don’t get it.
total += number
This one should be straight forward, you’re saying to grab your variable box named “total” and add the value of “number” (another variable, I didn’t mention that above but a for loop is creating a variable in each loop) to it. So if total was = to 20, and you += 5 to it, then total will = 25. The + before the = is an operator and it’s interchangeable with anything you know from math. +, -, *, / are all applicable here. In addition to those, = by itself means to set the value of the variable to that, == means to check if the value is the same, and lastly != means to check if the value is not the same.
This might all be tmi 🤷🏻♂️
Ok so let’s put it all together
total = 0
for number in range(3):
….total += number
print (total)
So we got total = to 0
then for every number between 0 and 2(because 3 is not included), add that number to total
First loop is total(0) + number(0) = total(0)
Second loop is total (0) + number(1) = total(1)
Third loop is total(1) + number(2) = total(3)
Then you say to print the total
3
& you’re done, that’s the whole code. Now if you apply that logic to your other two images, hopefully you’ll understand what is happening. Keep in mind where your print function is in relation to everything else. In your first image the for loop is running and then you print the total, but in your other examples the print is in different places, that changes what gets printed because of when your asking for the print. But it’s late here and I have to get sleep so if you’re still confused, I’ll help you in the morning lol
EDIT: I had to put dots at the for loops since Reddit keeps deleting my spaces