Using sets will be screaming fast if you have any volume of data

If you are willing to use sets, you have the isdisjoint() method which will check to see if the intersection between your operator list and your other list is empty.

MyOper = set(['AND', 'OR', 'NOT'])
MyList = set(['c1', 'c2', 'NOT', 'c3'])

while not MyList.isdisjoint(MyOper):
    print "No boolean Operator"

http://docs.python.org/library/stdtypes.html#set.isdisjoint

Answer from gahooa on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ trouble understanding while not
r/learnpython on Reddit: Trouble understanding While Not
February 16, 2017 -

I'm doing "automate the boring stuff" and was on an early lesson about while loops. I can't wrap my head around "while not." The code is below

name = ''
while not name:
     print('Enter your name:')
     name = input()
print('How many guests will you have?')

While my limited knowledge, I would write it instead as

name = ''
while name == '':
    print('Enter your name:')
    name = input()
print('How many guests will you have?')

My question is how can they mean the same thing? Both start with name = False/empty. The way I interpret the top code is, while name is not False/empty. The way I interpret the bottom is, while name is equal to False/empty. In my mind, the top would keep looping with any True value. Thanks for your help!

EDIT: formatting

EDIT 2: Finally figured it out. I'm posting this edit for prosperity sake, for any other programming ogres like me. As long as the condition line is True, the loop will run. If the condition is False, the loop will not run. Even though name is False, the condition line is True and the loop will go until something is inputted. Once something is typed in, name becomes True, which would make the condition not name False, ending the loop.

Thanks everyone for helping me understand that! It's so simple now that I see it. I'm super new to Python and made a rookie mistake.

๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
Appdividend
appdividend.com โ€บ how-to-use-while-not-in-python
While Not in Python
December 13, 2025 - The while not is a variation of the while loop that continues to execute a block of code as long as a specified condition is false in Python.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-while.html
While Loop
Here is an example that uses while/True to go through the numbers 0..9. This not the best way to generate those numbers; it just shows how if/break can serve instead of a while/test at the top of the loop.
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 - 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'.
๐ŸŒ
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
๐ŸŒ
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: ...
๐ŸŒ
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....
๐ŸŒ
C2
wiki.c2.com
While Not Done Loop
This site uses features not available in older browsers
๐ŸŒ
TOOLSQA
toolsqa.com โ€บ python โ€บ python-while-loop
Python While Loop | While True and While Else in Python || ToolsQA
August 6, 2021 - Unlike the "for" loop in python, the while loop does not initialize or increment the variable value automatically. As a programmer, you have to write this explicitly, such as "i = i + 2".
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-while-loop
Python While Loop - Learn By Example
April 20, 2020 - If the condition is false at the start, the while loop will never be executed at all.
๐ŸŒ
Luc
anh.cs.luc.edu โ€บ handsonPythonTutorial โ€บ whilestatements.html
3.3. While Statements โ€” Hands-on Python Tutorial for Python 3
In Python, while is not used quite like in English. In English you could mean to stop as soon as the condition you want to test becomes false.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ loops-in-python
Loops in Python - GeeksforGeeks
If we want a block of code to execute ... while loop in Python to do so. Code given below uses a 'while' loop with the condition "True", which means that the loop will run infinitely until we break out of it using "break" keyword or some other logic. ... Note: It is suggested not ...
Published ย  1 month ago
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ controlflow.html
4. More Control Flow Tools โ€” Python 3.14.3 documentation
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example: >>> while True: ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ none object and while loop confusion.
r/learnpython on Reddit: None object and While loop confusion.
June 23, 2022 -
Player_1 = None

While (not Player_1):
    Player_1 = input('your name')

I'm not new to python, I understand classes and functions etc..but this is really baffling me and I need some help understanding 'None' and the While loop.

I understand that 'not' negates a Boolean value, I get that piece, but None is not a Boolean value, I've searched this and None is its own Object and None is also not equal to False or True.

The while loop runs if the condition evaluates to true, and doesn't run if evaluated to False.

What is making the loop run the first time and not the second time. Obviously, the first time the logic equates to True and the second time to False...But I don't get the logic.

I think it's the "not" which is confusing me, what is the not doing?

Can someone please enlighten me?