In addition to if/else
, else
has some usecases in Python that can help with the flow control.
for / else
1 2 3 4 5 6
| for i in range(0,100): if i == lucky_num: print(f"lucky number is {i}") break else: print("No lucky number found")
|
try / else
1 2 3 4 5 6 7 8
| try: do_something() except: print('error') else: print('finished without error') finally: print('all finished')
|
Reference