File Description: Python Beginner Tutorial Six — While Loop

This is the last lesson of the beginner tutorial. After learning this lesson, we will have mastered the most basic foundation of Python. Great! In this section, we will learn about the while loop.

The while loop is a basic loop.

Basic form of while:
while (condition):
statement

The while statement can be simply memorized as: as long as the loop condition is true, the loop statement will execute.

Now let's look at an example as shown in Figure 1:

a = 0
while (a < 10):
a = a + 1
print(a)

It's very simple: assign 0 to a, then as long as a is less than 10, add 1 to a and assign it back to a, and finally print a.

Now let's learn two more statements: break and continue.

The break statement can jump out of a loop, that is, end the loop early and continue executing the statements below the loop.

The continue statement ends the current iteration and continues with the next iteration of the loop.

The code is shown in Figure 3. I won't explain this time; if you still don't understand, then review it multiple times or post a question.

With this, our Python beginner tutorial is finished. I hope you understand and learn it well because only by mastering it can we smoothly proceed to the intermediate tutorial.

I won't say much more.

Resources 5 and 6 are the code for this lesson, saved as .py files.



Replying is a virtue!

Visa status: Not required