Loops In Python

When we want to execute the same set of statements sequentially, for several times then we go for Looping.

A loop statement gives us the flexibility to execute set of statements n number of times with conditions as we set.

Python language provides below mentioned looping types :

1.While Loop
2.For Loop
3.Nested Loop

While loop : While loop ensures to repeat the set of statements, when given condition is TRUE. If the condition evaluates to TRUE, then the set of statements are executed or else will quit the while loop execution.
While Loop Syntax of Python:
While expression:

Statement(s)

Line 1 : While expression ( condition ) to evaluate

Line 2 : Statement(s) will contain one or more statements to be executed.

Note : Line 2 will be executed only if the Line 1 (while expression ) returns TRUE.

While Example of Python :

increament= 0

while increament < 3:
print(“Within while Loop”, increament)
increament = increament + 1

OUTPUT:

While Loop Example In Python

For loop in Python : For loop will execute a set of statements for multiple times as we have declared in For loop.

For Loop Syntax of Python :

for iteration_loop in sequence:

Statement(s)

Line 1 : for loop syntax where iteration_loop is a temporary variable and sequence contains a string.

Line 2 : After executing the Line1 the line 2 statements will be executed.

For Loop Example of Python :

for character in ‘language’ :

OUTPUT:

For Loop Example In Python

3.Nested Loops : Nested loops is defined as having multiple Loops within single loop. There can be instance where we have for loop within while loop.

Nested Loops Syntax of Python :

While expression :

While expression :

Statement(s)

Statement(s)
Line 2 : Checks for the condition when x=0

Line 3 : will print when the condition in Step 2 is satisified

Line 4: will execute when the condition is satisified if 4==4

Line 5 : Line 6 will be executed only when line 5 condition satisifies

Line 6 : X is increamented by count 1 when line 2 condition is satisified

Nested Loop Example of Python :

OUTPUT:

Loop Control Statements : Loop Control statements will change the execution flow from its normal behavior. There are three statements like

1.Break Statement
2.Continue Statement
3.Pass Statement

1.Break Statement : Will terminate the loop statement and will pass on the execution to the other loop. Most commonly we will be using Break statement with While and for loops.

Example on Break in Python:

Using Break statement to terminate the execution once If condition satisifies

for temp in “QACreators”:
if temp == “t”:
break
print(temp)

print(“Value Found”)

OUTPUT:

Python_Break_Statement_Example

2.Continue Statement : Continue statement will be used in situation where you have checked the condition and returns true. Instead of terminating the loop, still if you want to continue the loop execution we go for Continue statement.

Example on Continue In Python :

Usage of continue statement in loops

for temp in “QACreators”:
if temp == “C”:
continue
print(temp)

print(“Value found and execution continued”)
OUTPUT:

Python_Continue_Statement_Example

3.Pass Statement : Pass statement is used when we want to print the logs and do not want to perform any operation. We can use pass statements in Stubs to make it pass.

OUTPUT:

Leave a Comment

Your email address will not be published.