Search
Python Repetition Structure

Sometimes you may want to repeat something in a program.

Consider the following robotic questioning in python --

print("1 What is your name, sir?")
print("2 What is your name, sir?")
print("3 What is your name, sir?")
print("4 What is your name, sir?")
print("5 What is your name, sir?")
1 What is your name, sir?
2 What is your name, sir?
3 What is your name, sir?
4 What is your name, sir?
5 What is your name, sir?

It repeats the same question 5 times. However, if the program has to do this for hundreds -- if not millions -- of times, can you manage to type hundreds of python statements here? Perhaps you can, but it is going to be extremely tedious, to say the least.

Loop Basics

As we discussed, there is a programming structure called loop that can help you repeat doing something without typing the code over and over again. In particular, you can use a for loop to repeat something for a certain number of times.

The above Python code can be rewritten using a for loop --

for i in [1,2,3,4,5]:
    print(i, "What is your name, sir?")
1 What is your name, sir?
2 What is your name, sir?
3 What is your name, sir?
4 What is your name, sir?
5 What is your name, sir?

Here [1,2,3,4,5] is a list (array) of five numbers from 1 to 5, and i is a variable that repeats itself, each time taking a value from the list: the first time 1, the second time 2, ..., and finally the fifth time 5.

The above code can also be simplified as:

for i in range(1,6):
    print(i, "What is your name, sir?")
1 What is your name, sir?
2 What is your name, sir?
3 What is your name, sir?
4 What is your name, sir?
5 What is your name, sir?

Here, range(1,6) produces a list of numbers between 1 and 6 (6 not included), which is the same as [1,2,3,4,5].

With the range() function, it is easy to change the range and repeat for even more times. For example:

for i in range(1,100):
    print(i, "What is your name, sir?")

How many times does it repeat?

Loop for a Pattern

Now, consider this --

print("O "*1)
print("O "*2)
print("O "*3)
print("O "*4)
print("O "*5)
O 
O O 
O O O 
O O O O 
O O O O O 

When you use the expression "Some String" * n, where n is an integer, it repeats the string n times.

You can use a loop to do the same:

for i in range(1,6):
    print("O "*i)
O 
O O 
O O O 
O O O O 
O O O O O 

How about the following code?

for i in range(1,6):
    print("O "*(6-i))
O O O O O 
O O O O 
O O O 
O O 
O 

Explain what you see and why.

Nested Loop

Earlier, the code such as print("O "*3) repeats a string a number of times. This itself can be implemented with a loop such as:

for j in range(1,4):
    print("O ", end="")
O O O 

Here, to output 3 Os in the result, you use a loop with range(1,4), which is range(1, 3+1).

With this in mind, the above code can be rewritten as:

i = 3
for j in range(1,i+1):
    print("O ", end="")
print("")
O O O 

If you change i to 5:

i = 5
for j in range(1,i+1):
    print("O ", end="")
print("")
O O O O O 

You see the repetition (for j loop) code here is equivalent to print("O "*i), which repeats "O " several times depending on the value of i.

Remember this code:

for i in range(1,6):
    print("O "*i)
O 
O O 
O O O 
O O O O 
O O O O O 

where the above pattern is created with "O "*i expression in the loop?

We can replace the expression with our j loop, within the i loop:

for i in range(1,6):
    for j in range(1,i+1):
        print("O ", end="")
    print("")
O 
O O 
O O O 
O O O O 
O O O O O 

Compare the code. Does it make sense?

Now you see the j loop is a sub-strcture embedded in the i loop. This is a nested loop.

A nested loop is useful to process data and patterns in more than one dimensions. Here the two-level nested loop (i and j) creates a 2-dimensional pattern and can be used for two-dimensional data such as a matrix.