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
🌐
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.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-while.html
While Loop
Since the more commonly used for-loop automates the increment step for us, we don't quite have the muscle memory to remember it when writing a while-loop. Suppose there is some function foo() and you want a while loop to run so long as it returns True. Do not write this
Discussions

Trouble understanding While Not
An empty string coerces to False in a boolean context, while any other string coerces to True. The first version is the idiomatic way of writing that. More on reddit.com
🌐 r/learnpython
15
19
February 16, 2017
If we assign false to a variable and then use a while not loop, why is the variable still false and not true?
if we assign false to a variable and then use a while not loop, why is the variable still false and not true? More on discuss.python.org
🌐 discuss.python.org
0
0
November 16, 2022
How do while not loops work
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. More on reddit.com
🌐 r/learnpython
16
5
September 11, 2024
Is it weird that I NEVER use while loops? It’s just not something I’ve felt like I needed, ever. What are some situations where using a while loop is necessary?
Simplest example... Asking for user input, validating it, and asking it again if the user got it wrong, in a loop, forever, until he gets it right... For example I want only positive numbers, how would you do that with a for loop? More on reddit.com
🌐 r/learnpython
189
270
September 29, 2022
🌐
Python
wiki.python.org › moin › WhileLoop
While loops - Python Wiki
While loops, like the ForLoop, are used for repeating sections of code - but unlike a for loop, the while loop will not run n times, but until a defined condition is no longer met.
🌐
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.

🌐
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".
Find elsewhere
🌐
Appdividend
appdividend.com › how-to-use-while-not-in-python
While Not in Python
December 13, 2025 - The continue statement skips the current iteration of a loop and proceeds to the next iteration. x = 5 while not (x == 0): x = x - 1 if x == 3: continue print(x)
🌐
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.
🌐
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
🌐
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, ...
🌐
Coursera
coursera.org › tutorials › python-while-loop
How to Write and Use Python While Loops | Coursera
February 24, 2023 - Unlike for loops, they will repeat that block of code for an unknown number of times until a specific condition is met. You’ll use while loops when you’re not sure how many times you need your code to repeat.
🌐
Reddit
reddit.com › r/learnpython › is it weird that i never use while loops? it’s just not something i’ve felt like i needed, ever. what are some situations where using a while loop is necessary?
r/learnpython on Reddit: Is it weird that I NEVER use while loops? It’s just not something I’ve felt like I needed, ever. What are some situations where using a while loop is necessary?
September 29, 2022 -

I’ve written a lot of python code over the last few months, but I can’t think of a single time I’ve ever used a while loop. I mean, I use for loops pretty often, but not while loops.

Is this just down to personal preference, and I’m just using what I’m comfortable with? Or can you guys think of some situations where a while loop would be the easiest way to do things, but it’s possible to do it with a for loop? Maybe I’m using for loops in situations that I should be using while loops.

EDIT: Thanks for the suggestions. I found a few places in my code where a while loop makes sense.

First, I check if a folder has any files in it, and while it does, I delete the first one in the list:

useless_files = os.listdir("MGF_HR_Data") # I think these are files it creates when downloading the MGF data but they are extra and don't do anything 
while len(useless_files)>0: 
    os.remove(f"MGF_HR_Data/{useless_files[0]}") 
    useless_files = os.listdir("MGF_HR_Data") 
    print("Deleting useless file from MGF_HR_Data") 

I also used a while loop to check if the data has been downloaded, and if it hasn't prompt the user to download it and then press enter to check again. This way, the code doesn't break if the user forgot to download the file first:

# Check if you downloaded the file in Matlab already. If not, ask the user to download it and try again. 
while os.path.exists(file1)==False: 
    print(f"{file1} not found. Please open Matlab and run the following code to download the MGF data:" )
    print(f"download({wstime.year}, {wstime.month}, {wstime.day})")
    input("Press enter to try again after downloading the MGF data. \n") 

(Okay, I know this isn't the most efficient way to do this. There must be a way to use python to open matlab and run a matlab command, rather than asking the user to do it themselves. However, I don't know how to do that, so this works for now.)

🌐
Python.org
discuss.python.org › python help
Better while loop - Python Help - Discussions on Python.org
June 9, 2022 - Hello there 😚 I was wondering if there is a shorter or more functional way to write “while loops” in situations where you have to check multiple conditions. my problem is this while is way too long and it is hard to work with. while candle_riddle_ans.lower() != 'candle' and ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
If we want a block of code to execute infinite number of times then we can use the 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 to use this type of loop as it is a never-ending infinite loop where the condition is always true and we have to forcefully terminate the compiler.
Published   February 14, 2026
🌐
Medium
tiaraoluwanimi.medium.com › python-while-loops6420e2f913a3-6420e2f913a3
Python While Loops: Common Errors & How to Fix Them | Medium
November 10, 2021 - def times_table(number): #Only want to loop through 5 while multiplier <= 5: result = number * multiplier print(str(number) + "x" + str(multiplier) + "=" + str(result)) # Increment the variable for the loop multiplier += 1 times_table(3) ... Error: Oops! It doesn’t work. The error states, “local variable ‘multiplier’ referenced before assignment”. It simply means that we didn’t initialise our multiplier variable. The line “while multiplier <= 5:” cannot work in this case because our multiplier hasn’t been set. Likewise, the line “multiplier += 1”, which means “new multiplier = old multiplier + 1”, will not work because there’s no set “old” multiplier for our first iteration.
🌐
freeCodeCamp
forum.freecodecamp.org › python
Not sure why while loop is not iterating - Python - The freeCodeCamp Forum
November 28, 2021 - import time start_time = time.time() product = 0 num1 = 100 num2 = 100 while num1 <= 999: while num2 <= 999: product = num1*num2 print(product, num1, num2) product = str(product) if pr…
🌐
Initial Commit
initialcommit.com › blog › python-while-loop-multiple-conditions
Writing a Python While Loop with Multiple Conditions
March 11, 2021 - This Python while loop has multiple conditions that all need to be evaluated together. 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?
🌐
Texas Instruments
education.ti.com › en › resources › computer-science-foundations › do-while-loops
Python coding: Do While Loops | Texas Instruments
Python doesn’t have a built-in Do While loop structure, but the behavior of a Do While loop can be modeled with programs that use While, If and break commands as we’re exploring here. Let’s use an interactive notes page to formalize the Do While loop behavior that these structures model.
🌐
IONOS
ionos.com › digital guide › websites › web development › python while loop
How to use while loops in Python - IONOS
September 26, 2022 - The resulting code is much more complicated than the equivalent for loop. A while loop is clearly not the best solution for this problem: word = 'Python' letters = iter(word) letter = '' while letter is not None: letter = next(letters, None) if letter: print(letter)