0% found this document useful (0 votes)
15 views2 pages

Python Loop Else Clause Guide

The document explains the use of the else clause in Python loops, specifically with for and while loops. It describes how the else block executes after all iterations of the loop are completed. Examples are provided to illustrate the syntax and output of using else in loops.

Uploaded by

rohanbansiwal5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Python Loop Else Clause Guide

The document explains the use of the else clause in Python loops, specifically with for and while loops. It describes how the else block executes after all iterations of the loop are completed. Examples are provided to illustrate the syntax and output of using else in loops.

Uploaded by

rohanbansiwal5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Day35 Python - else in Loop

As you have learned before, the else clause is used along with the if statement.

Python allows the else keyword to be used with the for and while loops too. The
else block appears after the body of the loop. The statements in the else block
will be executed after all iterations are completed. The program exits the loop
only after the else block is executed.

Syntax
for counter in sequence:
#Statements inside for loop block
else:
#Statements inside else block

Example:
for x in range(5):
print ("iteration no {} in for loop".format(x+1))
else:
print ("else block in loop")
print ("Out of loop")

Output:
iteration no 1 in for loop
iteration no 2 in for loop
iteration no 3 in for loop
iteration no 4 in for loop
iteration no 5 in for loop
else block in loop
Out of loop

-----------------------main code-----------------

i = 0
while i<7:
print(i)
i = i + 1
# if i == 4:
# break

else:
print("Sorry no i")

for x in range(5):
print ("iteration no {} in for loop".format(x+1))
else:
print ("else block in loop")
print ("Out of loop")

You might also like