w3ajay

tally tutorial point, ms word 2013, ms excel 2013, ms powerpoint 2010,ccc question with answer in hindi 2021, Tally Prime in hindi , tally prime,Python,in python,programming in python,python

Saturday, December 11, 2021

loop else in python,Python else Loop

 Use else statement in loops

You can use the else statement in the for and while loops in Python. The else statement is optional and executes if the loop iteration completes normally. If the loop is terminated with a break statement, the else statement will not be executed.

flowchart



syntax 1 

For <variable> in <sequence>:

statement 1

statement 2

:

else:

         statement(s)


syntax 2 

while<test condition>:

statement 1

statement 2

:

     else :

statement (s)


Example 1

 for a in range(1,4)

print("Element is ",end=' ')

print(a)

    else :

Print("Ending loop after printing all element of sequence ")


Example 2


while a<4:

print("Element is ",end=' ')

print(a)

    else :

Print("Ending loop after printing all element of sequence ")

Example 3

for x in range(6):
  print(x)
else:
  print("Finally finished!")

Example 4


for currentLetter in 'Hello world!': 
   print ('The current letter is', currentLetter)
   if currentLetter == 'w':
       break
else:
   print ('All letters were printed.')


Read also these

No comments:

Post a Comment

for more information please share like comment and subscribe