Help Understanding While loops

Hello, I have been learning python using Solo learn. It has been a good source for me. I have made it to this point once and I restarted the course again. I am stuck at loops again. This seems to be an important part. If anyone could help me understand what is happening in this line of code I would appreciate it.

counter = 0 while counter < 4: print(counter) counter = counter + 1

Just to give a little background. This is a screenshot from solo learn.
I don’t understand the “+1” how that works.
I understand that it helps close the loop but I don’t know how.
The iteration. Is that the number of times the loop has cycled?
I have looked online for some answers I can find more complex problems that confuse me more. I think if I can understand this it will help to understand more complex lines of code. If anyone would be willing to take the time to help I would greatly appreciate it.
Thanks!

The way that python changes the value of a variable is to use =.
= does not mean equality as it does in mathematics.

To set counter to 0 you have this line of code:

counter = 0

Inside the loop the value of counter needs to be incremented by 1 each time.

counter = counter + 1

What that does is take the current value of counter, add one to it and replace the value in counter with the calculation.

So it counter is 3 then counter + 1 will be 4.

Does that help?

Thank you for your quick response.
So, in this case or all cases, the “=” sign does not mean that that is that value?
“counter” is holding that “0” value?

tell me if my mindset it right in how I understand this.

The code starts with 0 as long as its less than 0 it will print
the counter “counter = counter +1” creates the end point?
so it adds one to each “iteration” ?
and an iteration is a complete loop of the “loop” itself
it uses that to come up with the “4 = 3+1” and that is how it ends the loop?

I haven’t had that “ah ha” moment yet. I seems that I am missing an understanding of the equation. Thanks again for your help

The thing to understand is that there’s no such thing as an equation in Python. There are expressions and statements. What you are looking at here:

counter = counter + 1

is an assignment statement. The way to read this in general is “assign the value on the right hand side to the name on the left hand side”. On the first time through the loop, the value on the right hand side is 0 + 1, which evaluates to 1. Then the value 1 is stored with the name counter.

Do not get hung up on the equals sign. To avoid this exact confusion, some languages use <- for assignment to show that the value on the right is being put in the variable named on the left. Note that you cannot do the reverse:

>>> counter + 1 = counter
  File "<stdin>", line 1
    counter + 1 = counter
    ^^^^^^^^^^^
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?

If it were an equation, these would be equivalent. You can think of it as “make the left side equal to the right side”, but I would just get in your head that there’s no such thing as an equation.


So to break it down. you have an assignment statement:

counter = 0

Then you have a while loop:

while counter < 4:
    ...

This will check the value of the expression counter < 4. If the expression evaluates to True, then ... is run and the loop will start again with another check.

What does it run before the next check? First it will print out the value stored by the name counter.

    print(counter)

On the first time through the loop it will print 0 to the console. Every time you go through the loop, it will print out whatever value is stored by that name.

Then we assign a new value to the name counter:

    counter = counter + 1

The first time through the loop, we assign counter = 1. The second time through the loop (since 1 < 4) the value 1 will be printed to the console, and then we will assign counter = 2. And so on.

The iteration. Is that the number of times the loop has cycled?

so it adds one to each “iteration” ?

and an iteration is a complete loop of the “loop” itself

Your last question gets it right. An iteration is a time through the loop, and the number in your screenshot is just a count of the number of times you’ve been through the loop. This is just for your understanding; it doesn’t correspond to any Python concept (there’s no secret place where the iteration number is kept track of). Crucially the iteration number is entirely independent of any value. The correlation to the values of counter are entirely incidental.

“counter = counter +1” creates the end point?

All this does is update the value of counter. The loop stops because once counter points to the value 4, the check counter < 4 evaluates to False, and the while loop terminates.

it uses that to come up with the “4 = 3+1” and that is how it ends the loop?

Let’s replace the = with a <- for the sake of breaking the connection with equations. When counter has the value 3, then you can think of the increment line as going through the following transformations:

counter <- counter + 1  # Substitute the name counter for its value (3)
counter <- 3 + 1        # Add the two values (3 and 1)
counter <- 4            # Assign the value (4) to the name counter
2 Likes

I have only read the first two small paragraphs and already had an “ah ha” moment. I am going to keep reading. Thank you so much for this good information. This is was I was looking for. Thank you thank you given me a little hope in understanding this more and more.

Work slowed down and I was able to read the rest of your post. Thank you again. It makes so much more sense. I can picture it like the flow chart it would be and where the loop is after your explanation. Well put and thank you again. :partying_face:

1 Like