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
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.
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.
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
The result will be
If we add the break keyword
The result will be
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:

