In general, recall the while loop syntax. while : Conceptually, coding aside, consider filling a glass of water. Is there space for more water in the glass? Okay, then pour some. Without going in detail of how this funky glass object is defined, hopefully the following makes sense as a python analog of that process. while glass.has_space(): #we're still filling the glass glass.add_water() #glass is now full of water Without writing out how this glass object works, let's say it has another method for returning a boolean (True/False) for whether or not it is currently full, instead of the previous example of whether or not it had space for more water. We can fill the glass in the same way, but we have to negate that statement using the not operator while not glass.is_full(): #we're still filling the glass glass.add_water() #glass is now full of water Going back to the initial syntax, which is still the same, the only thing we've done is changing the condition statement of the loop. Instead of checking that glass.has_space() evaluates to True, we are now checking that the expression not glass.is_full() evaluates to True. That is the same statement as evaluating that glass.is_full() evaluates to False, because the only thing the not operator does to a boolean is negating it, i.e. True becomes False and vice versa. Now looking at your code linked, the condition for looping is that not sequenceCorrect evaluates to True, which is equivalent to the statement that sequenceCorrect is False. I won't paste the code here, but we see on line 3 that sequenceCorrect starts its life being False, so upon entering the while loop we do indeed step into that block of code because at that time not sequenceCorrect is in fact True. Then the first thing we do on line 5 is reassign it to True. If the remaining lines don't change it back to False, this will stop the while loop from repeating, since in this current state not sequenceCorrect evaluates to False. So you can say that line 5 is defaulting the loop to not going to be repeated. The only way for the loop to be repeated is for lines 6-8 with the nested for loop and if statement to find some character in dna that is not in "actgn", upon which sequenceCorrect will be assigned to False and thus the statement in the while loop not sequenceCorrect would evaluate to True and thus the loop would repeat itself once more. Personally, I think this is just a pretty unclear way to achieve the goal of checking the validity of that input string. I would have defined it another way, but I can't see anything wrong with it really. If the not operation in the while loop statement is bothering you, you could equally have rewritten the code with a variable for instance named sequenceIncorrect and just flipped all assignments i.e. True's become False's and vice versa. Answer from LatteLepjandiLoser on reddit.com
🌐
Reddit
reddit.com › r/learnpython › how do while not loops work
r/learnpython on Reddit: How do while not loops work
September 11, 2024 -

I was reading through this code and I'm just not getting how while loops work when not operator is also used.

https://pastebin.com/5mfBhQSb

I thought the not operator just inversed whatever the value was but I just can't see this code working if that's the case.

For example , the while not sequenceCorrect should turn the original value of False to True, so the loop should run while it's True. But why not just state while True then? And why declare sequenceCorrect = True again? Doesn't it just means while True, make it True? And so on.

The only way it makes sense to me is of the while not loop always means False (just like the while always means as long as it's True) even if the value is supposed be False and should be inverted to True.

So, is that the case? Can anyone explain why it works like that?

Top answer
1 of 8
7
In general, recall the while loop syntax. while : Conceptually, coding aside, consider filling a glass of water. Is there space for more water in the glass? Okay, then pour some. Without going in detail of how this funky glass object is defined, hopefully the following makes sense as a python analog of that process. while glass.has_space(): #we're still filling the glass glass.add_water() #glass is now full of water Without writing out how this glass object works, let's say it has another method for returning a boolean (True/False) for whether or not it is currently full, instead of the previous example of whether or not it had space for more water. We can fill the glass in the same way, but we have to negate that statement using the not operator while not glass.is_full(): #we're still filling the glass glass.add_water() #glass is now full of water Going back to the initial syntax, which is still the same, the only thing we've done is changing the condition statement of the loop. Instead of checking that glass.has_space() evaluates to True, we are now checking that the expression not glass.is_full() evaluates to True. That is the same statement as evaluating that glass.is_full() evaluates to False, because the only thing the not operator does to a boolean is negating it, i.e. True becomes False and vice versa. Now looking at your code linked, the condition for looping is that not sequenceCorrect evaluates to True, which is equivalent to the statement that sequenceCorrect is False. I won't paste the code here, but we see on line 3 that sequenceCorrect starts its life being False, so upon entering the while loop we do indeed step into that block of code because at that time not sequenceCorrect is in fact True. Then the first thing we do on line 5 is reassign it to True. If the remaining lines don't change it back to False, this will stop the while loop from repeating, since in this current state not sequenceCorrect evaluates to False. So you can say that line 5 is defaulting the loop to not going to be repeated. The only way for the loop to be repeated is for lines 6-8 with the nested for loop and if statement to find some character in dna that is not in "actgn", upon which sequenceCorrect will be assigned to False and thus the statement in the while loop not sequenceCorrect would evaluate to True and thus the loop would repeat itself once more. Personally, I think this is just a pretty unclear way to achieve the goal of checking the validity of that input string. I would have defined it another way, but I can't see anything wrong with it really. If the not operation in the while loop statement is bothering you, you could equally have rewritten the code with a variable for instance named sequenceIncorrect and just flipped all assignments i.e. True's become False's and vice versa.
2 of 8
2
The difference here is that by using not sequenceCorrect you are avoiding the use of the break keyword. It's cleaner and makes the loop terminate itself, rather than relying on a specific keyword and force terminate it. You could use while True, but that way is not descriptive enough and can potentially create and endless loop if you don't take care of the edge cases. Think about it: while not sequenceCorrect is telling you, without a single comment line, that the loop should run while sequenceCorrect is False, vs while True which the only thing that's telling you is that this loop will run endlessly for unknown reasons.
🌐
Stack Overflow
stackoverflow.com › questions › 65293083 › how-to-properly-use-while-not-loops-in-python
How to properly use "while not" loops in python? - Stack Overflow
This is to say it is basically "while guessed is false." So the while loop will run for as long as guessed is false and tries > 0. However, if guess is every made to be true, then the while loop will stop running.
🌐
Reddit
reddit.com › r/learnpython › trouble understanding while not
r/learnpython on Reddit: Trouble understanding While Not
February 16, 2017 - While not basically means While False and works as long as set condition is False. In this case we set name = False and say: as long as the name is False, i.e empty - do the loop.
🌐
Appdividend
appdividend.com › how-to-use-while-not-in-python
While Not in Python
December 13, 2025 - The while not statement in Python is used to execute a block of code repeatedly as long as a certain condition is false(condition is not met).
🌐
Real Python
realpython.com › lessons › not-in-while-loops
Using not in Boolean while Loops (Video) – Real Python
Just like with if, use not when there is a condition you want to be False for a loop to iterate, because writing a condition that should be True would be more complicated. 00:20 Suppose you’re writing a game or some other type of interactive ...
Published   July 19, 2022
🌐
Stack Overflow
stackoverflow.com › questions › 69599255 › confused-with-the-not-operator-in-while-loops-and-if-statements
python - Confused with the "not" operator in while loops and if statements - Stack Overflow
In Python if variable is empty, ... as long as chosen condition is True. While not basically means While False and works as long as set condition is False....
🌐
Python
wiki.python.org › moin › WhileLoop
While loops - Python Wiki
If the condition is initially false, the loop body will not be executed at all. As the for loop in Python is so powerful, while is rarely used, except in cases where a user's input is required*, for example:
Find elsewhere
🌐
Mimo
mimo.org › glossary › python › while-loop
Master Python While Loops: A Comprehensive Guide
While loops are very common in many Python applications. Here are some simple examples: The while loop is perfect for prompting users for input until they provide a valid response, making it a practical example of a conditional statement in action. For example, a program might repeatedly ask for a username until the input matches certain criteria. In this case, the username needs to be longer than four characters. ... username = None while not username: username_input = input ( "Enter a valid username: " ) if len (username_input) > 4 : username = username_input
🌐
C2
wiki.c2.com
While Not Done Loop
notice · javascript required to view this site · measured improvement in server performance · awesome incremental search
🌐
Quora
quora.com › Why-is-there-no-do-while-loop-in-Python
Why is there no 'do while' loop in Python? - Quora
Answer (1 of 7): Didn't do much research on it but a quick Google turned up with this answer and the answer seems legit considering it refference to PEP Python : Why there is no do while loop in python Here is it. Also in case you are trying to relate python and C dont. One is a language which ...
🌐
TOOLSQA
toolsqa.com › python › python-while-loop
Python While Loop | While True and While Else in Python || ToolsQA
August 6, 2021 - In case you are coming from another ... how to implement the same in python. Unfortunately, for "do while" fans, this loop is not supported by python....
🌐
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. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Real Python
realpython.com › python-not-operator
Using the "not" Boolean Operator in Python – Real Python
August 16, 2024 - These loops iterate while a given condition is met or until you jump out of the loop by using break, using return, or raising an exception. Using not in a while loop allows you to iterate while a given condition is not met. Say you want to code ...
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - The bad news is that Python doesn’t have one. The good news is that you can emulate it using a while loop with a break statement. Consider the following example, which takes user input in a loop: ... >>> while True: ... number = int(input("Enter a positive number: ")) ... print(number) ... if not number > 0: ...
🌐
Mimo
mimo.org › glossary › python › the-not-operator
Python Not Operator: Master Logical Negation | Learn Now!
password_correct = False while not password_correct: # The while loop iterates while the password isn't correct password = input("Enter your password: ") if password == "correct_password": password_correct = True else: print("Password incorrect, try again.") Countless real-world Python applications use the not operator for reversing conditions.
🌐
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 - Next, it is time to construct the while loop. I am going to check if the variable user_input is not equal to the contents of the variable secret_keyword. Essentially, I am checking whether what the user has entered is not equal to the string 'Python'.
🌐
Initial Commit
initialcommit.com › blog › python-while-loop-multiple-conditions
Writing a Python While Loop with Multiple Conditions
March 11, 2021 - The first condition checks whether count is less than a. The second condition checks whether count is less than b. The third condition uses the logical not operator to check that the value of count has not reached the maximum number of iterations. Python has set this value to a random number (here, max_iterations was 8). So, the loop runs for eight iterations and then terminates - but which condition caused this? Python evaluates conditional expressions from left to right, comparing two expressions at a time. One can use parentheses to better visualize how this works: while ( CONDITIONAL EXPRESSION A or CONDITIONAL EXPRESSION B ) and not CONDITIONAL EXPRESSION C