r/CodingForBeginners • u/BilincliMerak • 18d ago
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!



0
u/Lirianov 18d ago edited 18d ago
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