In the previous article, we learned about the while loop.

It is great when we do not know in advance how many times an action needs to be repeated.

For example:

  • waiting for the correct password;
  • repeating a program menu;
  • performing actions while a condition remains true.

But very often we already know that we want to go through some collection of data.

For this, Python provides the for loop.


The first for loop

The simplest example:

for number in range(5):
    print(number)

The result:

0
1
2
3
4

Python takes each number one by one and stores it in the variable number.

After that, it executes the body of the loop.


What is range()?

The range() function creates a sequence of numbers.

For example:

range(5)

means:

0 1 2 3 4

That’s why the loop:

for number in range(5):
    print(number)

can almost be read like a regular sentence:

For each number from the sequence 0, 1, 2, 3, 4, execute the following code.


Starting from a different number

For example:

for number in range(3, 8):
    print(number)

The result:

3
4
5
6
7

The first number is the start of the sequence.

The second number is the boundary that is not included in the result.

This is one of Python’s important characteristics.


Changing the step

By default, numbers increase by one.

But the step can be changed.

For example:

for number in range(2, 11, 2):
    print(number)

The result:

2
4
6
8
10

The third argument defines how much the number changes after each iteration.


for works with more than just numbers

The for loop can iterate not only over the result of range().

For example, it can iterate over a string.

for letter in "Python":
    print(letter)

The result:

P
y
t
h
o
n

Each iteration receives the next character from the string.

Later, we will see that the same idea works with lists, tuples, sets, and dictionaries.


When to use for and when to use while?

Often the choice is quite simple.

Use for when you need to iterate over elements of some sequence.

For example:

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

Use while when repetition depends on some condition.

For example:

  • waiting for the correct password;
  • repeating a program menu;
  • performing actions until the user gives a command.

What we learned today

Today we learned about the second type of loop.

  • for iterates over elements of a sequence.
  • range() creates a sequence of numbers.
  • You can specify the start, end, and step.
  • for can work not only with numbers but also with other data, such as strings.

Try it yourself

Try running these programs.

Print numbers from 1 to 10:

for number in range(1, 11):
    print(number)

Print only even numbers:

for number in range(2, 21, 2):
    print(number)

Print all characters of your name:

name = input("Enter your name: ")

for letter in name:
    print(letter)

Try changing the number range and step, as well as entering different names.


What comes next?

Now we know how to repeat actions and make decisions.

But so far, we have only worked with individual values.

In real programs, data is often stored in groups.

For example:

  • a shopping list;
  • a list of users;
  • a list of grades.

In the next article, we will learn about the first collection in Python — lists (list).