Explanation
From this link it explains that for loops can have an else statement.
From the link:
The else clause executes after the loop completes normally. This means that the loop did not encounter a break statement.
So in your first example the loop hits the break point and so doesn't execute the else clause print("Here"). In the second example the last value of i in the loop is 4 so the break is never hit.
Your examples
# Example 1
for i in range(10):
if i == 5:
print("Break")
break
else:
print(i)
else:
print("Here")
# Output: 0 1 2 3 4 Break
# Example 2
for i in range(5):
if i == 5:
print("Break")
break
else:
print(i)
else:
print("Here")
# Output: 0 1 2 3 4 Here
# Another example that doesn't cause break
for i in range(3):
if i == 5:
print("Break")
break
else:
print(i)
else:
print("Here")
# Output: 0 1 2 Here
elsedoesn't always pair withif. It can also go with afororwhileloop, or atry...exceptconstruction.else:in aforloop meansif no break. They just did not want to introduce a new keyword.