(Python) Loops. Loop with counter - for


Loops

Imagine a situation where we need to display the word "Hello" 10 times. What should we do?
You can take and write the command 10 times print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")

But what if you need not 10 times, but 20, 30, 40 times? And if 200 times? In this case, copying will take a very long time. And if it is necessary that the user himself could choose how many times he needs to display some information on the screen? 

A special construction will help us to cope with this task, which is called loop.
 
A loop - is an algorithmic construction in which a certain sequence of commands is repeated several times.

In most programming languages, there are two types of loops: a loop with a variable (for) and a loop with a condition (while)
Let's start our acquaintance with cycles from the first type.
 

Loop with a variable, or with a known number of steps (loop for)

It often happens that we know the number of repetitions of any action, or we can calculate the number of repetitions using the data known to us. Some programming languages ​​have a command that is written in Russian by the command
REPEAT (number_of_repetitions).

In this case, we can specify a specific number of repetitions. 
It is interesting to see how this cycle works at the machine level:
1. a certain memory cell is allocated in memory and the number of repetitions is written to it,
2. when the program executes the loop body once, the contents of this cell (counter) is decremented by one.
3. loop execution ends when this cell is zero.

There is no such construct in Python, but there is a for.
 
Need to remember!
The general form of the for loop operator is as follows:
for <count variable> in <values ​​of counter variable>:
    # loop body

1. The name of the counter variable can be anything (more often they use the letter i)
2. The loop body is written with a shift to the right, relative to the line with the loop header (for). The PEP8 standard shifts by 4 spaces.
 
Let's see examples of how you can write the output of the word "Hello" 10 times.
 
Example 1
for i in range(10): # for variable i mutable in range (in range)
  # 0 to 10 (10 is not included)
    print("Hello") # print Hello
The   variable is a counter of completed iterations (steps) of the loop. In this case, the loop runs for i = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. That is exactly 10 times.
The same program can be written differently.
 
Example 2
for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
    print("Hello")
In example 2, we list all the values ​​of the variable i for which we need to loop. With a large number of values, it is more convenient to use the built-in function range().
The sequence of values ​​built by the range() function is finite. The loop will always end. 

Features of the for

loop How to change the step in the sequence of values ​​and not start from scratch? The  range() function, by default, builds a sequence in which each next number is 1 greater than the previous one. You can use the range function in another entry.

The general form of the function entry is as follows:
range([start], stop[, step])
  • start: start number of the sequence.
  • stop: generates numbers up to but not including the given number.
  • step: the difference between each number in the sequence (step)

You have to remember!
  • All parameters must be integers:
  • Each of the parameters can be either positive or negative.
  • range() (and Python in general) is based on index 0. This means that the index list starts at 0, not 1.  The last integer generated by the function  range() depends on stop but will not include it. For example, range(0, 5) generates the integers 0, 1, 2, 3, 4, not including 5.


Example 1
for i in range (10, 0, -1):
    print(i*i)
The program displays the squares of natural numbers from 10 to 1 in descending order
  • 10: The first number in the sequence.
  • 0: end number of the sequence (not including this number).
  • -1: step


Example 2
for i in range (0, 101, 5):
    print(i)
The program displays all numbers from 0 to 100 in increments of 5
  • 0: The first number in the sequence.
  • 101: end number of the sequence (not including this number).
  • 5: step

Repeat N times

All programs with a for loop that we have written so far cannot be called universal. Because we ourselves set the number of repetitions of the loop body. 
But what if the number of repetitions depends on some other value? For example, the user himself wants to set the number of repetitions of the cycle.
What to do in this case?
Everything is very simple. Instead of numeric start and end values, we can use any variables that can be calculated or set by the user.

For example, we need to display the squares of numbers from 1 to N, where the value of the variable N is entered from the keyboard by the user.
The program will look like this:
N = int(input()) # input N from the keyboard
for i in range(1, N+1): # loop: for all i from 1 to N  - variable i
                           # will sequentially take values ​​from 1 to N
  print("square", i, "=", i*i)  # print the square of a number

When entering the loop, the assignment statement i = 1 is executed, and then the variable i is incremented by one with each step (i += 1). The loop is executed while the condition i <= N is true. In the body of the loop, the only output statement prints the number itself and its square on the screen according to the specified format.
For squaring or other low exponents, it's better to use multiplication.

Run the program and see the result of its work with different values ​​of the variable N.