for else
for/else clause could be useful when you go through iterable for searching purpose.
If
breakisn’t called insideforloop,elsecaluse will be execute.If
breakis called insideforloop, skipelseclause.
Example:
ls = [1, 2, 3]
for i in ls:
print(i)
else:
print("Excute else")
# Output:
1
2
3
Excute else
Example:
for i in ls:
print(i)
if i == 3:
print("Execute break")
break
else:
print("Excute else")
# Output:
1
2
3
Execute break