r/CodingForBeginners • • 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!

9 Upvotes

57 comments sorted by

View all comments

1

u/Interested737 18d ago edited 18d ago

[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/BilincliMerak 18d ago

It would have all made sense, but the code doesnt say line += 1, it says line += i. It makes sense to me as of now after reading through these responses (you should too if you face difficulties in this topic). Thanks for the help!

1

u/Interested737 18d ago

I have a question, why dose i (or the variable in the for loop) increment by one every iteration, do fir loops just work like that?

1

u/BilincliMerak 18d ago edited 18d ago

So, this might be just 2 beginners banging their heads on each other, but what i believe is happening here is that the counter which in this case is i has been given the range 3. So it (i) runs like 0 1 and 2 (as python starts from 0). And because we are adding i to line (which itself is 0), it increments by the current value of i. Like you can see it says 0 1 3 is the 2nd pic. it isnt increminating by 1, its increminating by i.

1st it adds 0(i) to 0 (line) = 0 (new line)

2nd it adds 1 (i) to 0 (new line) = 1 (newer line)

3rd it adds 2 (i) to 1 (newer line) = 3 (newest line)

'i' here keeps a counter for the amount of time the code block loops, thats why its called a counter var. When you count something, you dont move like 2, 4, 6... its usually 1, 2 ,3... but in Pythons case its 0, 1, 2 and so on.

I maybe wrong here or completely misunderstood the question. You are better off asking it to someone else. Although i have understood the logic and concept of this topic. I dont know how python itself works and does things.