For Loop

Python SELF EN
Level 4 , Lesson 2
Available

8.1 For Loop

It's awesome that you can run a set of commands based on a condition. But what if I want to do something 100 times? How do we do that?

For this, Python has loops. And for our task, a for loop is perfect. The general form of the for statement is like this:

for variable in value_list:
    command1
    command1
    commandN

Where value_list is some set of values: a list, a string, a dictionary (more on them later).

The variable sequentially takes values from value_list, and for each of them, the block of commands is executed.

Example:

Example Explanation

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
1. The variable fruit is assigned the value "apple" and the command print(fruit) is executed 2. The variable fruit is assigned the value "banana" and the command print(fruit) is executed 3. The variable fruit is assigned the value "cherry" and the command print(fruit) is executed

This is how the loop works:

If there are 10 values in the list after the word in, then the block of commands will be executed 10 times.

For each value from the value_list, two things happen:

  1. The loop variable (which is specified after the word for) is assigned the next value from the value_list.
  2. Then the block of commands is executed.

Another example:

Example Explanation

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for x in numbers:
    print(x)
The loop will print the numbers:
1
2
3
4
5
6
7
8
9
10

8.2 Specifying a Range — range

In the last example, you could see that printing 10 numbers is very simple. But what if we want to loop 100 times? Or 1000?

Of course, there's a solution for this. In Python, there's a special function range() for generating a sequence of numbers, which in combination with a for loop makes it a powerful tool for handling repetitive tasks.

The function range() can be used in several ways, depending on the number of arguments passed to it.

Basics of using range():

One argument: range(n) generates a sequence from 0 to n-1. This is useful when you need to perform an action a certain number of times.


for i in range(5):
    print(i)  # Prints numbers from 0 to 4

Two arguments: range(start, stop) generates a sequence from start to stop-1. This is useful for starting iteration from a specific number.


for i in range(1, 6):
    print(i)  # Prints numbers from 1 to 5 

Three arguments: range(start, stop, step) adds a third argument step, which determines the step between numbers in the sequence.


for i in range(0, 10, 2):
    print(i)  # Prints even numbers from 0 to 8  

Reverse loops: range() can generate numbers in reverse order, allowing for reverse iterations.


for i in range(10, 0, -1):
    print(i)  # Prints numbers from 10 to 1 

The range() function, in combination with the for loop, provides a flexible tool for controlling iterations in Python. Understanding its capabilities helps create more efficient and readable loops, which are an integral part of any program.

8.3 Looping through a List

If you want to perform a set of actions on different values, the range() function doesn't quite fit; you can always group these values in a list using square brackets. This operation generally looks like this:


for variable in [val1, val2, …, valN]:
    command1
    command1
    commandN

You simply list the values separated by commas, and the loop will execute for each of them, with the variable sequentially taking each value. Example:

Example Explanation

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
                    
The loop will print the strings:
apple
banana
cherry

You can specify numbers in the list, right in the loop after the word in:

Example Explanation

for price in [0.99, 2.75, 0.25]:
    print(price)
The loop will print the numbers:
0.99
2.75
0.25

You can list objects of any type in the list:

Example Explanation

for data in [0.99, "apple", -1, True]:
    print(data)
                
The loop will print the strings:
0.99
apple
-1
True
2
Task
Python SELF EN, level 4, lesson 2
Locked
Cyclotron
Cyclotron
2
Task
Python SELF EN, level 4, lesson 2
Locked
The Clever Raja
The Clever Raja
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION