Python: Loop

A loop is a control structure in high-level programming languages designed to organize the repeated execution of a set of instructions. A loop can also refer to any repeatedly executed sequence of instructions organized in any manner (e.g., using conditional jumps with labels).

Loops are initially divided into 2 types:

A definite loop (for) is a loop in which the number of repetitions of actions is known in advance.

An indefinite loop (while) is a loop in which the number of repetitions of actions is not known in advance, but the condition for ending the loop is known.

Python: function range()

Before discussing the for loop operator, let's consider the range function. We will need it to organize the loop.

range() is a versatile Python function for creating lists (list) that contain an arithmetic progression. It is most commonly used in a for loop. The range() function can take one to three arguments, which must be integers (int).

range([start,] stop[, step]) – this is the standard call of the range function.

By default, start = 0 and step = 1.

It returns a list of integers in the form [start, start + step, start + step*2...].

If the step is a positive number, the last element of the list will be the largest start + i * step that is less than the stop value. Here, i is the number of steps.

If the step is a negative number, the last element will be the smallest start + i * step that is greater than the stop value.

The stop value is not included in the list. The step value must not be zero, otherwise a ValueError (error) will occur. Moreover, if the step is a positive number, start must be less than stop, and if the step is a negative number, start must be greater than stop.

Examples of using the range function:

Code Range of numbers
range(5) [0, 1, 2, 3, 4]
range(2, 5) [2, 3, 4]
range(0, 10, 2) [0, 2, 4, 6, 8]
range(0, -5, -1) [0, -1, -2, -3, -4]

Python: loop for

Let's consider a definite loop, also known as a counting loop. This loop is implemented using the for loop operator. The for loop in Python iterates over objects in the sequences passed to it, such as lists, strings, and ranges.

The counter variable is assigned an initial value from the range.

The block of code inside the loop is executed.

If not all values from the range have been iterated over, the counter variable takes the next value from the range and proceeds to step 2; otherwise, it exits the loop.

If the break operator was not used inside the loop, the block of code in the else statement is executed after the loop finishes. This block is optional.

Note that, as with other operators, the block of actions must be indented consistently.

At this stage of study, we will use only one sequence, which is called a range (range). We will consider the others when we study strings, lists, etc.

Code: Example loop for


>>> for index in range(1, 6):
        print(index) 
1
2
3
4
5 

>>> for index in range(0, 11, 2):
        print(index) 
0
2
4
6
8
10

>>> for index in range(10, 0, -1):
        print(index)
10
9
8
7
6
5
4
3
2
1

            

Python: loop while

The while loop is an indefinite loop because it can be used in cases where the number of times a block of actions will be repeated is not known in advance. A while loop in Python executes a block of program code as long as the condition specified in the loop evaluates to True.

The conditions in the loop are built according to the same principle as those considered in the branching algorithm.

Sequence of execution of the while loop:

1. The variables are assigned initial values.

2. The condition is checked, and if it is true, the block of code inside the loop is executed, otherwise the loop execution ends.

3. Variables change as required by the task.

4. Go to point 2.

5. If the break statement was not used inside the loop, then after the loop is completed, the block of code after the else statement will be executed. This block is optional.

Code: Example while for


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

    
# Output:    
# 1
# 2
# 3
# 4
# 5 

            

Links: Python documentation


[1] Python loop while


[2] Python loop for