How Python almost had another keyword (16/100 Days of Python)

Martin Mirakyan
3 min readJan 17, 2023

--

Day 16 of the “100 Days of Python” blog post series covering while … else and for … else statements

In Python, the while and for loops can be used with an else clause that allows you to specify a block of code that will be executed after the loop completes its execution. This can be useful when you want to check for certain conditions after the loop has finished running and take appropriate action based on the outcome:

i = 0
while i < 5:
print(i)
i += 1
else:
print('The loop has finished executing.')

In this code, the while loop will iterate through the numbers 0 through 4. After the loop has finished executing, the else block will be executed and the message "The loop has finished executing" will be printed.

You can also use the for...else statement in a similar way:

for i in range(5):
print(i)
else:
print('The loop has finished executing.')

In this code, the for loop will iterate through the numbers 0 through 4. After the loop has finished executing, the else block will be executed and the message "The loop has finished executing" will be printed.

It’s important to note that the else block will only be executed if the loop completes its execution normally. If the loop is exited prematurely using a break statement, the else block will not be executed.

for i in range(5):
if i == 2:
break
print(i)
else:
print('The loop has finished executing.')
# Output: 0,1

for i in range(5):
if i == 6:
break
print(i)
else:
print('The loop has finished executing.')
# Output: 0,1,2,3,4, The loop has finished executing.

At some point, during the discussions, an alterntive name was proposed for this behavior. Some people suggested to have a name nobreak instead of an else to make it more intuitive. The name nobreak would especially make sense in case offor loops. Yet, a decision was made to use an already existing else keyword. The else keyword makes a lot of sense in case of a while loop where the contents of the else statement would be executed in case of the while condition evaluating to Flase, which is very similar to what would happen if we had an if statement instead of a while.

The while...else and for...else statements are very useful when searching for a value in a collection — where the program should do some action in case the value is found, and something totally different if it was not:

names = ['James', 'Bob', 'Anna', 'Lily', 'Mary']
username = input('Please enter a name: ')

for name in names:
print(f'Looking at name `{name}`')
if name == username:
print('Yey! The name you entered is in the list!')
break
else:
print('Sorry, the name you entered was not found...')

So, the while...else and for...else statements in Python allow you to specify a block of code that will be executed after the loop completes its execution. This can be useful when you want to check for certain conditions after the loop has finished running and take appropriate action based on the outcome. However, the else block will only be executed if the loop completes its execution normally. This is especially useful when searching for an item in a list — so the program would do something else if the item is not found.

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

No responses yet