Saturday, 9 September 2017

for loop with else in python

for loop with else:

Example1:

for i in range(5):
    if i<5:
        print("hai")
else:

    print("hello")

Output:

hai
hai
hai
hai
hai

hello

Example2:

for i in range(5):
    if i==2:
        break
    print("hai")
else:

    print("hello")


Output:

hai
hai

Example3:

for i in range(5):
    if i==2:
        continue
    print("hai",i)
else:
    print("hello")

Output:

hai 0
hai 1
hai 3
hai 4
hello

No comments:

Post a Comment