for-else.py
for fruit in ['banana','apple','orange']:
#If you break out of the loop in the middle, else will not be executed
#if fruit ==apple:
# break
print('I ate ' + fruit)
else:
print('I ate all!')
This else is more like a try-except relationship than if-else. However, there are documents that are deprecated, such as Effective Python, because it is difficult for humans from other languages to understand.
Recommended Posts