Module: (Python) Loops. Loop with counter - for


Problem

1/15

Loops in programming

Theory Click to read/hide

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. 

Problem

  1. Modify the program so that it displays the word Hello 10 times in a column.
  2. Run the program and make sure it prints the word Hello 10 times.