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
Syntax of for loops in Python
Syntax of for loops in Pythonfor value in sequence: {loop body}
The Example of for loops in Python
Example of for loops in Python#for loop book = ["maths", "science", "English", "Social Science"] for a in book: print(a)
Let’s run the code
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
Syntax
Syntax of while loops in Pythonwhile conditional_expression: Code block of while
The Example of while loops in Python
Example of while loops in Python#while loop i = 1 n = 10 while i <= n: print(i) i = i + 1
Nested loops
A loops mean loops inside a loop.
Syntax
Syntax of Nested loopsfor element in sequence for element in sequence: body of inner for loop body of outer for loop
The Example of Nested loops
Example of Nested loops# nested loop for i in range(6, 14): for j in range(1, 11): print(i * j, end=' ') print()
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
0 Comments