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