while True in Python creates an infinite loop that runs continuously until explicitly terminated using a break statement, return, or an exception.

  • The condition True always evaluates to True, so the loop body executes repeatedly without end.

  • To exit the loop, you must include a break statement inside the loop body based on a specific condition (e.g., user input, a flag, or a logical check).

  • It is commonly used for:

    • Interactive programs (e.g., command-line tools that accept user input until the user types "exit").

    • Event listeners or servers that run continuously.

    • Input validation loops that keep prompting until valid data is entered.

Example:

while True:
    user_input = input("Enter 'quit' to exit: ")
    if user_input == "quit":
        break
    print(f"You entered: {user_input}")

⚠️ Caution: Always ensure a break condition exists. Without it, the loop runs forever, consuming resources and potentially freezing the program.

while True means loop forever. The while statement takes an expression and executes the loop body while the expression evaluates to (boolean) "true". True always evaluates to boolean "true" and thus executes the loop body indefinitely. It's an idiom that you'll just get used to eventually! Most languages you're likely to encounter have equivalent idioms.

Note that most languages usually have some mechanism for breaking out of the loop early. In the case of Python it's the break statement in the cmd == 'e' case of the sample in your question.

Answer from Richard Cook on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › can someone explain what this `while true` function is actually checking?
r/learnpython on Reddit: Can someone explain what this `while True` function is actually checking?
December 13, 2023 -

https://pastebin.com/NWkKQc1P

It's from Automate the Boring Stuff. I tried running it through the python tutor and it didn't clarify. Is the basic point that there actually isn't anything it's checking to be True, so it's an infinite loop until you trigger the `break`?

PS I figure this is something simple from much earlier that I didn't internalize. I plan to go through all the basic curriculum again to make sure there aren't any gaps in super basic knowledge.

Top answer
1 of 8
23
A while loop runs while a condition is true. True is always true, so it will run until something causes it to break (e.g. a break statement, program crash, etc.) For comparison, the first loop in the code you posted could be rewritten without an infinite loop as: age = input("Enter your age: ") while not age.isdecimal(): age = input("Please enter a number for your age: ")
2 of 8
9
Correct; this is just a way to start an infinite loop that you intend to break with some condition/break statement later on. Mostly used for starting/running an application that you don't want to close down without user intent (clicking an exit/quit button in a UI or entering "quit", "exit", etc from the command line. It *isn't* a great construct to use when you want something that has solidly defined criteria to repeat until those criteria are met. As mopslik pointed out, you would be better served in those examples to actually use the defined criteria in those as your loop conditions since you're not at risk of just sticking the user in an infinite loop if you forget to write your break statement somewhere. You, also, don't have to write so many lines to accomplish the same goal. ``` password = "" while not password.isalnum(): password = input('Select a new password (alphanumeric only') print("Password is good") ``` You do not have to print a line and then call input(), either. Just put the message you want displayed into the parens for the function call: input("Enter your age: ")
Discussions

Python : While True or False - Stack Overflow
What do you expect the True and False to do here? They are just references to the boolean values on a line, they are otherwise discarded here. ... @MartijnPieters I expect, if 'False' , it quits the while loop and the script continue to my other loop which I didn't include More on stackoverflow.com
🌐 stackoverflow.com
Is using while True loops good?
Having a proper end condition for the while loop makes it more readable, and easier to understand. More on reddit.com
🌐 r/learnpython
19
10
December 3, 2022
What actually "while true" statement mean can someone pull me out this?
What actually "while true" statement mean can someone pull me out this · In practical terms, it will loop a code block, while a particular condition is True. Said condition may never be True, in which case you get an infinite loop, or you can set and test a variable so that you have a fixed ... More on discuss.python.org
🌐 discuss.python.org
0
0
April 30, 2022
"While(true)"? Why does this work?
susan corbett is having issues with: Is it because the value it's testing is the loop's return value? saying essentially "While this loop returns true, do this action?" So "... More on teamtreehouse.com
🌐 teamtreehouse.com
4
December 6, 2015
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-use-while-true-in-python
How to use while True in Python - GeeksforGeeks
July 23, 2025 - In Python, loops allow you to repeat code blocks. The while loop runs as long as a given condition is true. Using while True creates an infinite loop that runs endlessly until stopped by a break statement or an external interruption.
🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Bootcamp Python Certificate Python Training ... With the while loop we can execute a set of statements as long as a condition is true....
🌐
Board Infinity
boardinfinity.com › blog › use-while-true-in-python
Use While True in Python | Board Infinity
August 13, 2025 - In conclusion, the "while True" loop is a useful tool in Python programming that allows a block of code to be repeated indefinitely. It is often used with a break statement to exit the loop under certain conditions.
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › while-loops-in-python-while-true-loop-statement-example
While Loops in Python – While True Loop Statement Example
July 19, 2022 - If the condition evaluates to True, then the loop will run the code within the loop's body and continue to run the code while the condition remains True. It will keep executing the desired set of code statements until that condition is no longer ...
🌐
YouTube
youtube.com › watch
Python While True Loop Explained with 3 Easy Code Examples 2025 - YouTube
Learn how to use the Python while True loop in this beginner-friendly tutorial! 🚀 We'll break down what a while True loop is, why it's useful, and show you ...
Published   January 21, 2025
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - The basic syntax of a while loop is shown below: ... In this syntax, condition is an expression that the loop evaluates for its truth value. If the condition is true, then the loop body runs.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-while.html
While Loop
Given that we have if/break to get out of the loop, it's possible to get rid of the test at the top of the loop entirely, relying on if/break to end the loop. We do this by writing the loop as while True:...
🌐
Python.org
discuss.python.org › python help
What actually "while true" statement mean can someone pull me out this? - Python Help - Discussions on Python.org
April 30, 2022 - What actually "while true" statement mean can someone pull me out this · In practical terms, it will loop a code block, while a particular condition is True. Said condition may never be True, in which case you get an infinite loop, or you can ...
🌐
Coursera
coursera.org › tutorials › python-while-loop
How to Write and Use Python While Loops | Coursera
If the test expression evaluates to true, the body of the while loop will be entered. This process repeats until the test expression evaluates to false. ... 1 2 3 4 5 numbers = [2, 4, 6, 8, 10] total = 0 for num in numbers: total += num print("The ...
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
As well as the while statement just introduced, Python uses a few more that we will encounter in this chapter. if Statements: Perhaps the most well-known statement type is the if statement. For exa...
🌐
TOOLSQA
toolsqa.com › python › python-while-loop
Python While Loop | While True and While Else in Python || ToolsQA
August 6, 2021 - The while loop in python is a way to run a code block until the condition returns true repeatedly. Unlike the "for" loop in python, the while loop does not initialize or increment the variable value automatically.
🌐
Readthedocs
pc-microbit-micropython.readthedocs.io › en › latest › lessons › while_True_loops.html
2. while True loops — PC-Microbit-Micropython
2. while True loops · Previous Next · See: `<https://www.w3schools.com/python/python_while_loops.asp · While loops run a set of statements as long as a test condition is true. while True: loops run forever. Instead of using a condition that returns True or False, True is used in place of ...
🌐
Sololearn
sololearn.com › en › Discuss › 3060225 › what-is-the-difference-between-while-and-while-true-in-python
what is the difference between "while" and "while True " in Python? | Sololearn: Learn to code for FREE!
July 14, 2022 - 'while' is a keyword 'while True' is a complete while statement with a boolean value. The 'while' keyword needs to be followed by a condition that will return a boolean value. Syntax: 🔸 while condition: code In your question, True is already ...
🌐
IONOS
ionos.com › digital guide › websites › web development › python while loop
How to use while loops in Python - IONOS
September 26, 2022 - Some well-known examples are ATMs, the Linux command prompt and the Python Read-Eval-Print loop (REPL). Below we show a sketch of an REPL implementation using an infinite while loop. # Loop while True: # Read user input user_input = read_input() # Evaluate input and produce result result = eval(user_input) # Print result print(result)
🌐
Quora
quora.com › What-does-while-true-do-in-python
What does 'while true' do in python? - Quora
Answer (1 of 12): Thanks for A2A While True means loop for ever. While loops, are used for repeating a section of code until the defined condition is met. [code]q = 0 while q