for -- else: what was the motivation?
rbowman
bowman at montana.com
Sat Oct 8 01:49:34 EDT 2022
On 10/7/22 21:32, Axy wrote:
> So, seriously, why they needed else if the following pieces produce same
> result? Does anyone know or remember their motivation?
In real scenarios there would be more logic in the for block that would
meet a condition and break out of the loop. If the condition is never
met, the else block runs. To steal from w3schools:
fruits = ["apple", "peach", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
else:
print("Yes we got no bananas")
More information about the Python-list
mailing list