Julius Nyerere

Machine Learning engineer with a deep understanding of the end-to-end machine learning lifecycle.

Julius Nyerere

Machine Learning engineer with a deep understanding of the end-to-end machine learning lifecycle.

Julius Nyerere

Machine Learning engineer with a deep understanding of the end-to-end machine learning lifecycle.

Blog Image
Blog Image
Blog Image

Jul 30, 2025

4min read

The Secret Python Keyword That Would Have Changed How You See Loops

As I was doing my daily reading, I encountered an interesting phenomenon. I dug further and was pleasantly surprised by what I found out. This article will focus on the nobreak keyword, a Python keyword that almost became a reality.

TL;DR

Nobreak is an unofficial keyword suggested by Raymond Hettinger, a well-known Python core developer. The context for this suggestion revolves around the counterintuitive behavior of the else clause in Python’s for and while loops. When used with these loops, the else block is executed only if the loop completes without encountering a break statement.

Let’s dissect this.

Refresher on Control Flow Statements

Control Flow Statements in Python dictate the order in which a program’s instructions are executed. The three types of control statements are:

1. Conditional Statements — Executing a block of code if a specified condition is true and another block if the condition is false, i.e, if-else

# == Test for even numbers == #number = 5if number % 2 == 0:    print(f'{number} is an even number')else:    print(f'{number} is an odd number')

2. Loop Statements — Executes a block of code x amount of times, i.e, for loop— a specified number of times, while loop— as long as a condition is true, run this.

# == Printing a numbers below 5 == #     count = 0   while count < 5:       print(count)       count += 1 # Increment count in each iteration
       # Example of a for loop iterating over a list   fruits = ["apple", "banana", "cherry"]   for x in fruits:       print(x)   # Example of a for loop using range()   for i in range(5): # Iterates from 0 up to (but not including) 5       print(i)

3. Jump statements — Used to alter the normal sequential flow of execution within loops and functions. i.e, break — Terminates a looping statement and transfers control to the statement immediately following the loop. They are useful when you want to check for a certain condition, and if the condition is met, the loop stops, and you move on to the next section of the code.

count = 0for i in range(156,179):    count += 1    if i == 160:        last_number = i        break    print(i)print("The number of times the code run is ",count, "and the last no is ", last_number)
156157158159The number of times the code run is  5 and the last no is  160

Where does ‘nobreak’ come in….

You own a raffle ticket company, and various players submitted their lucky numbers. You want a program that iterates through submitted numbers, and if there’s a winner, we want to break the loop and pass control to the next block of code. It would look like this without the break

lucky_number = 11  # Our lucky numbersubmitted_numbers = [24, 2, 4, 6, 22, 11, 3, 1]for num in submitted_numbers:    if num == lucky_number:        print('🎉 We have a winner! Raffle #', num)        #without a break statementelse:    print('🤷‍♂️ All tickets checked.!')

The result will be

🎉 We have a winner! Raffle # 11🤷‍♂️ All tickets checked

If we add the break keyword

lucky_number = 11  # Our lucky numbersubmitted_numbers = [24, 2, 4, 6, 22, 11, 3, 1]for num in submitted_numbers:    if num == lucky_number:        print('🎉 We have a winner! Raffle #', num)        break  #we have added a 'break' to pass control to the printelse:    print('🤷‍♂️ All tickets checked.!')

The result will be

🎉 We have a winner! Raffle # 11

Did you notice something? I hope you did. Remember what a break keyword is supposed to do? It terminates a looping statement and transfers control to the block of code immediately following the loop. The next block of code is the print(‘🤷‍♂️ All tickets checked!’) statement. Control hasn’t been passed to it. This behavior is contrary to what break statements are intended to do.

Here’s the kicker

When used with these loops, the else block is executed only if the loop completes without encountering a break statement, which is counterintuitive to how else statements should work. If the loop is exited prematurely using a break statement, the else block will not be executed. The discussion centered around introducing a nobreak keyword, which would be a clearer and more semantically appropriate keyword to indicate that the associated code block runs when the loop finishes normally (i.e., “no break” occurred).

Interesting right?

For more interesting tidbits, follow me on:

Data, AI and BeyondData, AI & Beyond explores the exciting new world of data, from foundational concepts in data science and machine…medium.com


LET'S WORK
TOGETHER

LET'S WORK
TOGETHER

LET'S WORK
TOGETHER

Create a free website with Framer, the website builder loved by startups, designers and agencies.