The else clause is only executed after the while condition is evaluated to be false.

Thus, if you break out of the loop, or if an exception is raised, the else won't be executed (since the while condition has not been evaluated to be false yet).

One way to think about it is as an if/else construct with respect to the condition:

if condition:
    handle_true()
else:
    handle_false()

is analogous to the looping construct:

while condition:
    handle_true()
else:
    # condition is false now, handle and go on with the rest of the program
    handle_false()

An example might be along the lines of:

while value < threshold:
    if not process_acceptable_value(value):
        # something went wrong, exit the loop; don't pass go, don't collect 200
        break
    value = update(value)
else:
    # value >= threshold; pass go, collect 200
    handle_threshold_reached()
Answer from ars on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ while loops can have an else statement!
r/learnpython on Reddit: While loops can have an else statement!
January 31, 2024 -

Maybe I'm the only one who didn't know this, but you can put an else statement after a while loop which will be used if the while loop is never entered. I found this out on the Leetcode problem of the day for today when I tried putting an else after my while loop, mostly expecting an error but typing because it's what I wanted the code to do, and it ran! The code I used is below. I know there are more optimal solutions, but I'm happy with what I learned today.

class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
    n = len(temperatures)
    answers = [0] * n
    stack = []
    for i in range(n):
        while len(stack) > 0 and temperatures[i] > stack[-1][1]:
            answers[stack[-1][0]] = i - stack[-1][0]
            stack.pop()
        else:
            stack.append([i, temperatures[i]])
        if len(stack) == 0:
            stack.append([i, temperatures[i]])
    return answers

๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ til you can use else with a while loop
r/Python on Reddit: TIL you can use else with a while loop
March 1, 2025 -

Not sure why Iโ€™ve never heard about this, but apparently you can use else with a while loop. Iโ€™ve always used a separate flag variable

This will execute when the while condition is false but not if you break out of the loop early.

For example:

Using flag

nums = [1, 3, 5, 7, 9]
target = 4
found = False
i = 0

while i < len(nums):
    if nums[i] == target:
        found = True
        print("Found:", target)
        break
    i += 1

if not found:
    print("Not found")

Using else

nums = [1, 3, 5, 7, 9]
target = 4
i = 0

while i < len(nums):
    if nums[i] == target:
        print("Found:", target)
        break
    i += 1
else:
    print("Not found")
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-while-else
Python While Else - GeeksforGeeks
July 23, 2025 - In Python, the while loop is used for iteration. It executes a block of code repeatedly until the condition becomes false and when we add an "else" statement just after the while loop it becomes a "While-Else Loop".
๐ŸŒ
Medium
allwin-raju.medium.com โ€บ understanding-the-while-else-loop-in-python-436c791649e8
Understanding the while-else Loop in Python | by Allwin Raju | Medium
November 12, 2024 - But did you know that Python allows you to pair an else statement with a while loop? The while-else combination can be useful in scenarios where you want to execute code after a loop completes, but only if the loop wasnโ€™t interrupted by a break.
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python basics โ€บ python while else
Python while...else Statement
March 31, 2025 - The else clause in the while else statement will execute when the condition of the while loop is False and the loop runs normally without encountering the break or return statement.
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_while_else.asp
Python While Else
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else
๐ŸŒ
Vaia
vaia.com โ€บ en-us โ€บ explanations โ€บ computer-science โ€บ computer-programming โ€บ python-while-else
Vaia | The #1 learning app for university & school
February 25, 2026 - Vaia is an intelligent learning app for students. Achieve your learning goals with more structure, motivation & efficiency. โญ Sign up now!
๐ŸŒ
Real Python
realpython.com โ€บ lessons โ€บ while-loop-else-clause
The While Loop Else Clause (Video) โ€“ Real Python
This lesson covers the while-loop-else-clause, which is unique to Python. The else-block is only executed if the while-loop is exhausted. You donโ€™t know what that means? Check out this lesson to find out!
Published ย  March 1, 2019
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python โ€บ python for else
Python For Else - Scaler Topics
December 1, 2023 - In the try statement, the else ... to for-else. ... Else block in for-else or while-else will only execute once when the loop completes all its iterations without termination by a break statement...
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - The control then returns to the loop header, where the condition is re-evaluated. The loop continues until number reaches 0, at which point it terminates as before. ... Python allows an optional else clause at the end of while loops.
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ python โ€บ python-else-loop
Python else Loop
Python allows else keyword to be used with for and while loops too. The else block appears after the body of the loop. Statements in else block will be executed after all iterations are completed.
๐ŸŒ
TOOLSQA
toolsqa.com โ€บ python โ€บ python-while-loop
Python While Loop | While True and While Else in Python || ToolsQA
August 6, 2021 - If there is a while loop in python with a break statement and an else statement, the else statement skips when the"break" executes. It would be a good exercise to run a while loop in Python with a break and else statement.
๐ŸŒ
Study.com
study.com โ€บ computer science courses โ€บ computer science 113: programming in python
Else Statements in Loops in Python: Definition & Examples | Study.com
This can be used very effectively while writing your scripts in Python so use it wherever you can! To unlock this lesson you must be a Study.com member Create an account ยท Python supports an else clause in for and while loops.
๐ŸŒ
Tutorjoes
tutorjoes.in โ€บ python_programming_tutorial โ€บ while_else_and_for_else_in_python
While Else and For Else in Python
This program demonstrates the use of the else block in both while loops and for loops in Python.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ while-loop
Python while Loop (With Examples)
In Python, a while loop can have an optional else clause - that is executed once the loop condition is False.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-else-loop
Python Else Loop | GeeksforGeeks
July 28, 2020 - The else block is executed at the end of loop means when the given loop condition is false then the else block is executed. So let's see the example of while loop and for loop with else below.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_while_loops.asp
Python While Loops
With the break statement we can stop the loop even if the while condition is true: ... Note: The else block will NOT be executed if the loop is stopped by a break statement.