All Courses

Loops in Python

Pallavi Dhotre

3 years ago

Loops in Python | insideAIML
Table of Content
Introduction
Python Loops
Python loop statements
  • while loop (do while loop )
  • Pre-test loop
  • Post-test loop
  • For loop
  • Nested loop
Summary

Introduction 

          Do you know how to perform repetitive tasks in python? To perform the repetitive tasks, python loops are used. But before that, you should identify the need for loops and check that whether it is possible to use a sequential control structure for getting the result? Then you should identify how many times the task will be repeated. This repetition is also known as iterations. The numbers of iterations are performed using the python loop statement. Suppose, you want to print any particular message 1000 times, will you use the print() statement 1000 times? The answer is no. Because this can be achieved using python loop statements. In this article, we are going to discuss in-depth python loops and python loop statement with examples. 

Python Loops

          Repeating one or more tasks until they met a specified condition is called a loop control structure. There are two types of loops: finite loop structure and infinite loop structure. In finite loops after a certain condition met loop exits. In infinite loops conditions never meet and the loop never exits. Let’s take a brief look at the python loop statement: 

Python Loop Statements

          Following are some python loop statement  
  • while loop 
  • for loop 
  • nested loop 
Let’s have a brief look at the python loop statement

while loop (do-while loop )

          Python doesn’t provide support for a do-while loop. Post-loop structure while loop can be used instead of the do-while loop in python. The while loops are based on two important aspects, one is condition and the second is a block of statements. If the condition is true then the block of statement gets executed and again control goes to check the condition. This process repeats until the condition results in false. There are some rules for writing the while loop. You should initialize the variable before using it in the while loop. Then variable must be altered inside the while loop. While loops are divided into two parts: pre-test loop and post-test loop. Let’s see one by one. 

Pre-test loop

          In pre-test loop structure the first condition is evaluated and then the block of statements gets executed.
For example:
python loops - while loop example
#pre-test loop
i=1
while i<4:
    print(i)
    i+=1
Output:
1

2

3

Post-test loop

          In the post-test loop structure, the first statements or blocks of statements get evaluated, and then the condition is executed. Condition with break statement is used in a post-test loop structure.
For example:
#python loops - while loop example
#post-test loop
i=1
while True:
    print(i)
    i+=1
    if i>4:
        break
Output:
1

2

3

4

For loop

          The ‘while loop’ can be finite as well as an infinite loop. But if are aware of the number of iterations that time for loop is used.  The range() function is used with for loop in python. There are three parameters for the range() function. First is the initial or starting range, second is the end of the range, and third is a gap. The gap should not be zero when using the range function. The initial range should be smaller than the end range while using a positive integer as a gap. While using a negative integer as a gap initial range should be greater than the end range.  See the following example:
#python loops - for loop example
#for loop without range()
print("Without using range() function")
for i in [1,2,10]:
    print(i) #this will print all numbers in the given list
print("using range() function with positive integer as a gap")
for i in range(5,15,3): #using the positive integer as a gap
    print(i) # this will print the numbers from 5 to 15 (excluded 15) with a gap of 3
print("using range() function with negative integer as a gap")
for i in range(15,5,-3): #using the negative integer as a gap
    print(i) # this will print the numbers from 15 to 5 (excluded 5) with a gap of 3
Output 
Without using range() function
1
2
10
using range() function with positive integer as a gap
5
8
11
14
using range() function with negative integer as a gap
15
12
9
6

Nested loop

          Inner loops enclosed within the outer loop are known as a nested loop in python. All types of loop control structures can be used as inner and outer loops. The only thing you should remember is inner loop should be closed before closing the outer loop. 
For example:
#python loops - nested loop example
for i in range(5,15,2): #outer loop
    while i<10: #inner loop
        print(i) # end of the inner loop
        break
print("The end") #end of the outer loop
Output 
5

7

9

The end
Also, we can use a decision control structure inside the nested loops. See the following example for more details:
#python loops - nested loop example
for i in range(5,15,2): #outer loop
    if i<10: #inner loop
        print(i) 
    else:
        print(i+2)
        # end of the inner loop
print("The end") #end of the outer loop
Output 
5

7

9

13

15

The end

Summary

          In this article, we discussed loops in python and python loop statements. To perform the repetitive tasks, python loops are used. The numbers of iterations are performed using the python loop statement. There are two types of loops: finite loop structure and infinite loop structure. In the finite loops after a certain condition is met loop exits. In infinite loops conditions never meet and the loop never exits. There are three python loop statements. They are while loop, for loop, and nested loop. We hope you enjoyed the article. If you have any related queries, feel free to ask in the comment section below. 
   
Enjoyed reading this blog? Then why not share it with others. Help us make this AI community stronger. 
To learn more about such concepts related to Artificial Intelligence, visit our insideAIML blog page.
You can also ask direct queries related to Artificial Intelligence, Deep Learning, Data Science and Machine Learning on our live discussion forum.
Keep Learning. Keep Growing. 
  

Submit Review