In the previous article, we learned how to make programs make decisions.

For example:

age = 20

if age >= 18:
    print("Access granted")

But what should we do when an action needs to be performed many times?

For example:

  • asking for a password until it is correct;
  • repeating a program menu;
  • waiting until the user enters a valid value.

This is where loops come in.


Repeating actions

Imagine that we want to print numbers from 1 to 5.

Without a loop, we would have to write:

print(1)
print(2)
print(3)
print(4)
print(5)

It works, but it is inconvenient.

And what if we need to print numbers from 1 to 1000?

This is exactly why loops exist.


The first while loop

A while loop means:

While the condition is true, execute the code.

For example:

count = 1

while count <= 5:
    print(count)
    count = count + 1

The result:

1
2
3
4
5

Let’s see what happened.

First:

count = 1

We created a variable with the value 1.

Then Python checks the condition:

count <= 5

As long as it is true, the block inside while is executed.


Why did the loop stop?

This line is very important:

count = count + 1

Each iteration of the loop increases the number.

The sequence becomes:

1 → 2 → 3 → 4 → 5 → 6

When count becomes 6, the condition

count <= 5

becomes false.

The loop ends.


Infinite loops

What happens if we forget to change the value?

For example:

count = 1

while count <= 5:
    print(count)

The condition always remains true.

The program will keep printing:

1
1
1
1
...

Such a loop is called an infinite loop.

That’s why a while loop usually needs something that eventually changes the condition.


Stopping a loop with break

Sometimes we need to stop a loop earlier.

For this, Python provides the break statement.

For example:

while True:
    answer = input("Enter a command: ")

    if answer == "exit":
        break

    print("You entered:", answer)

Now the program will continue running until the user enters:

exit

After that, the loop will stop.


Skipping an iteration with continue

Sometimes we don’t want to stop the loop.

We only want to skip the current iteration and move to the next one.

For this, we use continue.

For example:

number = 0

while number < 5:
    number = number + 1

    if number == 3:
        continue

    print(number)

The result:

1
2
4
5

When number became 3, Python skipped the print() call and immediately started the next iteration.


What we learned today

Today we learned about the first loop in Python.

  • while repeats code while a condition is true.
  • The condition is checked before every iteration.
  • The state inside a loop must change, otherwise you can create an infinite loop.
  • break completely stops a loop.
  • continue skips the current iteration and moves to the next one.

Try it yourself

Write a program that asks for a password.

For example:

password = ""

while password != "python":
    password = input("Enter password: ")

print("Access granted")

Try:

  • entering an incorrect password several times;
  • entering the correct password;
  • adding a limit on the number of attempts.

What comes next?

Now we know how to repeat actions while a condition is true.

But sometimes we already know that we want to go through a collection of elements.

For example:

  • printing all numbers from 1 to 10;
  • processing a list of users;
  • iterating through characters of a string.

For this, Python provides the for loop, which we will explore in the next article.