For Else Operation
For Else Operation#
[2]:
l = "hello My Name is Nishant Baheti".split(" ")
l
[2]:
['hello', 'My', 'Name', 'is', 'Nishant', 'Baheti']
[3]:
for item in l:
print(item)
else:
print("this loop ran successfully")
hello
My
Name
is
Nishant
Baheti
this loop ran successfully
if there is a break then else will not run
[4]:
for item in l:
print(item)
break
else:
print("this loop ran successfully")
hello
[6]:
for item in l:
if item == "Nishant":
print("Nishant found")
break
else:
print("Nishant was not found")
Nishant found