Loops in Python Programming
Loops in Python
In this lesson, we will learn what is loops in Python programming and their types.
What is Loops
Loops in Python programming come into use when we need to repeatedly execute a block of statements. For example: Suppose we want to print Hello World 10 times on the screen. This can be done in two ways as shown below:
Iterative Method
In Iterative method we have to write the printf() function 10 times in our program as shown below.
Example
print('Hello World')
print('Hello World')
print('Hello World')
print('Hello World')
print('Hello World')
print('Hello World')
print('Hello World')
print('Hello World')
print('Hello World')
print('Hello World')
Output
Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World
Here you can see that we have written Hello World 10 times using print() function. So, this will print Hello World 10 times on the screen. But if want to print Hello World 100 times on the screen then the iterative method is not suitable for this. To do this we have to use loop in our program.
Loop Method
In the Loop method, we do not have to write the Hello World 10 times in our program rather we have to write Hello World only once and the loop will execute the statement 10 times.
Types of Loop in Python
There are 2 types of loop in Python and they are:
- for Loop
- while Loop
In our next lessons we will learn more about these loops in great detail with examples.