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 OverflowI'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.
Videos
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
The expression 'AND' and 'OR' and 'NOT' always evaluates to 'NOT', so you are effectively doing
while 'NOT' not in some_list:
print 'No boolean operator'
You can either check separately for all of them
while ('AND' not in some_list and
'OR' not in some_list and
'NOT' not in some_list):
# whatever
or use sets
s = set(["AND", "OR", "NOT"])
while not s.intersection(some_list):
# whatever
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?
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?
Which is the recommended syntax? "while <list> is not None:" make more sense to me than "while <var>:" Same thing for "if <list> is not None". However, which way of writing is more proper or according to PEP8?