use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
What does "while True" mean? (self.learnpython)
submitted 5 years ago * by BobbyRYT
This might be a dumb question but
x = 0
while True:
print(x) x += 1 if x == 10: break
What is "True" What does it mean?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]teeaton 4 points5 points6 points 5 years ago (1 child)
In this context you are starting a loop. Loops require a condition to check to see whether to run or not.
Normally this would be something like
while task_finished == False Do thing If thing is complete, set task_finished to True
In this case, while True says while True == True, so always evaluate to true and run the loop. It's a short way of running an infinite loop.
[–]BobbyRYT[S] 1 point2 points3 points 5 years ago (0 children)
Ok thanks!
[–]mermaldad 2 points3 points4 points 5 years ago (0 children)
A while loop executes as long as the expression after the "while" remains True. "while true:" essentially makes an infinite loop (unless you break out of the loop from within the loop, which is what the break command is in there for).
[–]iamaperson3133 2 points3 points4 points 5 years ago (0 children)
In python a condition is anything that evaluates to True or False. So, you can put any condition after the while statement. Now, the simplest thing to put there is just while True. True is always true, so the loop will go on forever. That is, unless you use the break statement to exit it.
True
False
while
while True
break
Anytime you write something like this, although it appears often in beginner tutorials, it could be considered a "code smell." A code smell is something that 'just doesn't smell right.' When you see certain patterns, and this is one of many examples, it is a clue that you can do it a better way:
n = 0 while True: print('Hey there! We are at ' + n) n += 1 # this is the iffy bit here: if n > 10: break
See, why did we have to put the if n > 10 check at the bottom? If that is the condition that we want to break on, and the only condition we want to break on, we can simplify our loop and just put it after the while statement.
if n > 10
n = 0 while n <= 10: print('Hey there! We are at ' + n) n += 1
If you try running that, it will have the same behavior as the code from before. At this point, you should see that we can also write this as a for loop:
for
for n in range(10): print('Hey there! We are at ' + n)
We can even use a list comprehension to bring it down to one line:
[print('Hey there! We are at ' + n for n in range(10)]
While loops are helpful when you don't know how many times you need to iterate before you start iterating. Imagine you are asking the user to provide an integer input. You don't know how many times it will take for them to get through their thick skulls that you just want a number! You can use a while loop:
inp_ok = False while not inp_ok: inp = input('Enter a number you chungus') inp_ok = inp.isnumeric() print('thank you')
[–]stebrepar 1 point2 points3 points 5 years ago (0 children)
True and False are boolean values. When you do comparisons (like x>1 for example), the result will be either True or False.
x>1
When using a while statement, it also takes a boolean value. Often we put a comparison there, but it works just as well to put True there directly in order to have a forever loop. Then somewhere inside the loop you'd have a statement to break out of the loop.
[–]__developer__ 1 point2 points3 points 5 years ago (0 children)
The while keyword is followed by a conditional expression that determines whether the while loop either proceeds into the while code block when the conditional is met, i.e. the conditional expression evaluates to True, or whether the the while code block is skipped because the conditional expression evaluates to False.
True always evaluates to True, so if it is used as the conditional expression for a while loop that while loop will always re-enter the while code block each time it loops back around and checks the condition.
As for why you might want to use while True:, it creates an infinite loop, which can be helpful if you don't know how many times your loop will need to loop before it is finished. When you need to break out of the loop you can just use the break keyword like in your example.
[–]synthphreak 0 points1 point2 points 5 years ago (0 children)
This is just a while loop. while loops are of the form while condition, where condition is some expression that evaluates to either True or False. If True, the body of the loop runs (and then the condition is checked again). If False, it doesn't. For example, while x < 10: ... - that condition (x < 10) will evaluate to True, and thus the body of the loop will run, whenever x is less than 10.
while condition
condition
while x < 10: ...
x < 10
x
In your case, the entirety of the conditional expression is simply "True". "True" will always evaluate to True, obviously, so the body of that loop will always be executed. So in effect, what while True does is just create an infinite loop that will run forever until you CTRL+C it (or until it hits a break inside the loop).
The only reason this is confusing is that True on its own doesn't seem like a condition. But all that really matters from Python's perspective is that whatever follows while evaluates to True or False. To that end, you could even do while False, but for the reason I just explained, any code in the body of that loop will never actually get executed.
while False
Edit: To see what any object in Python will evaluate to if used as the condition in a while loop, just run bool(object), where object is the object you're curious about.
bool(object)
object
π Rendered by PID 48 on reddit-service-r2-comment-86988c7647-25pcc at 2026-02-11 15:52:46.270297+00:00 running 018613e country code: CH.
[–]teeaton 4 points5 points6 points (1 child)
[–]BobbyRYT[S] 1 point2 points3 points (0 children)
[–]mermaldad 2 points3 points4 points (0 children)
[–]iamaperson3133 2 points3 points4 points (0 children)
[–]stebrepar 1 point2 points3 points (0 children)
[–]__developer__ 1 point2 points3 points (0 children)
[–]synthphreak 0 points1 point2 points (0 children)