In this tutorial, we will learn about the Loop in Python, or its types, and how to use them with the help of examples.

Loop is the process to repeat the given statement again & again loop having a condition & it will repeat till condition is true.

Types

There are two types of loop

  • Entry Controlled Loop
  • Exit Controlled Loop

Entry Controlled Loop

This category loop having condition at start stage of its structure, so it checks condition first if true then execute. If this loop founds condition at initially false then it will repeat 0 time.

There are two types of Entry controlled loop

  • For Loop
  • While Loop

Exit Controlled Loop

This Category loop having condition at end stage of tis structure, so it first repeat then check the condition if the given condition is initially false then it will repeat 1 time atleast.

  • Do While Loop

Python provides following types of loops

  • For loop
  • While loop
  • Nested loop

For Loop

This loop is use for repeating the statement at desire number of times. This loop is useful with many data types such as number, string, list, tuple, etc.

Flow Diagram of for loop in Python

Flow Diagram of for loop in Python
Flow Diagram of for loop in Python

Syntax of for loops in Python

for value in sequence:  
    {loop body}   
Syntax of for loops in Python

The Example of for loops in Python

#for loop
book = ["maths", "science", "English", "Social Science"]
for a in book:
  print(a) 
Example of for loops in Python

Let’s run the code

Example of for loops in Python
Example of for loops in Python

While Loop

A loop statement in repeatedly executes a target statement as long as a given condition is true.

Flow Diagram of while loop in Python

Flow Diagram of while loop in Python
Flow Diagram of while loop in Python

Syntax

while conditional_expression:  
    Code block of while  
Syntax of while loops in Python

The Example of while loops in Python

#while loop
i = 1
n = 10
while i <= n:
    print(i)
    i = i + 1
Example of while loops in Python
Example of while loops in Python
Example of while loops in Python

Nested loops

A loops mean loops inside a loop.

Syntax

for element in sequence 

   for element in sequence:
       body of inner for loop
   body of outer for loop
Syntax of Nested loops

The Example of Nested loops

# nested loop
for i in range(6, 14):
    for j in range(1, 11):
        print(i * j, end=' ')
    print()
Example of Nested loops
Example of Nested loops
Example of Nested loops

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about Python Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *