Loops in Python
Last updated Aug 10, 2021
In Python Programming, Python Loops are used for iterating a sequence (which is either a
list, a dictionary, a tuple, a set or a string). With Loops in python we can execute a set of
statements, once for each time in a list, dictionary, tuple, set etc.
Types of Loops in Python Programming Language
In Python Programming, there are 3 types of loops which handles looping requirements. They
are stated below:-
- For Loop
- While Loop
- Nested Loop
We will understand all the loops statements one by one briefly.
For Loop
What is For Loop in Python?
The Loops which are used for Sequential traversal are known as For Loop. The syntax of For
Loop in Python Programming language is similar to the syntax of all other languages. Given
below is the syntax of For Loop in Python:-
for iterator_var in sequence: |
Python For Loop Example
1. Using range function
We will understand use of for loop using range function.
n = 5
for i in range(0,n):
print(i)
|
OUTPUT:-
0
1
2
3
4
From above code, you can see that we had used for loop with range function and we know that our range function runs from 0 to n-1 terms. So it has printed from 0 to 4 instead of 0 to 5.
Using for loop in a list
We will understand the use of for loop with the help of lists
name = ['rohan', 'sumit', 'jerry']
for i in name:
print(i)
|
OUTPUT:-
rohan
sumit
jerry
In the above code we had used list in for loop and we will get all the element of list as output in separate line respectively
Using For Loop in String
Python program to print characters from string with for loop
for i in "jerry": print(i)
|
OUTPUT:-
jerry
As it is visible from the above code, we can see that all the elements of the string are printed one by one in separate line respectively.
While Loop
The loop which can execute a given set of statements as long as the given condition is true is known as While Loop in Python Programming language. But when the condition is false then the line immediately after the loop in program is executed. Let's understand the syntax of While loop in Python Programming language
while test_expression: Body of While
|
Example of While Loop
We will understand the use of While loop with the help of a basic or general example
i = 0
while i<5:
print(i)
i = i + 1
|
OUTPUT:-
0
1
2
3
4
The above explained example is the general case of while loop
Using While Loop with Break Statement
Let's understand the use of While loop with break statement with the help of an example
i = 0
while i<5:
print(i)
if i == 2:
break
i = i + 1
|
OUTPUT:-
0
1
2
As you can see from the above code that we had used break statement which stops the code after printing 2
Using While loop with Continue Statement
Let's understand the use of While loop with continue statement with the help of an example
i = 0
while i<5:
print(i)
if i == 2:
continue
i = i + 1
|
OUTPUT:-
0
1
2
2
2
2
2
2
2
2
2
2
2
2
2
We had used continue statement in the above code and we can see that our code will execute and will run infinite times as it is in the loop
Nested Loops
The function of nested loops is similar to the function of nested If- Else statements and in simple words it means that loop inside a loop. We can use nested for loops and nested while loops
We will understand the use of Nested For Loop with the help of a example
f_name = ["sumit", "rohan", "jerry"]
l_name = ["kumar", "singh"]
for x in f_name:
for y in l_name:
print(x, y)
|
OUTPUT:-
sumit kumar
sumit singh
rohan kumar
rohan singh
jerry kumar
jerry singh
From above code we can see that we had used two for loops and we can easily see from the output that each f_name is printed with each l_name. We can also use nested For loops to print a number of different patterns
Nested While Loops
We will understand the use of Nested While Loop with the help of a example
i = 0
j = 5
while i < 4:
while j < 9:
print(i, "," , j)
j = j + 1
i = i + 1
|
OUTPUT:-
0 , 5
1 , 6
2 , 7
3 , 8
From above code it is clearly visible that we had used to while loops which is a nested loop and it will perform the same functions which are similar to the functions of nested For loop and will give us the output in the same way like nested for loop.
Thanks for Reading. Hope you liked it. Happy Learning!!!
Article Contributed By :
|
|
|
|
88 Views |