2

Basically my question is does saying not affect the whole if statement, even if I added an or or and in it, or does it affecting only the part that does not begin with an or or and?

EDIT:

Some people tried to send me another similar question(thanks for the help!), but it was talking about the hierarchy, while I was talking about whether or whether not the not affects the whole if-statement.

Bored Comedy
  • 187
  • 9
  • 3
    Operator precedence: https://docs.python.org/3/reference/expressions.html#operator-precedence – jarmod Jan 21 '20 at 04:24
  • 2
    Does this answer your question? [Priority of the logical statements NOT AND & OR in python](https://stackoverflow.com/questions/16679272/priority-of-the-logical-statements-not-and-or-in-python) – mrEvgenX Jan 21 '20 at 04:25
  • 1
    `not` is an operator that affects an expression according to the rules of precedence. – juanpa.arrivillaga Jan 23 '20 at 01:17

1 Answers1

1

So the way python statements work is that the not will only effect the next true/false statement it encounters. not a and b would look for a case where a is false and b is true, while not (a and b) would look for a case where both a and b are not true at the same time. You could even do b and not a and that would give an identical result to not a and b

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Karan Shishoo
  • 2,402
  • 2
  • 17
  • 32
  • 3
    While `b and not a` might yield the same result as `not a and b`, they are not identical. Python has short-circuit boolean operators: `and` is a short-circuit operator, so it only evaluates the second argument if the first one is true. – jarmod Jan 21 '20 at 04:27
  • @jarmod true, ill edit it to make it a bit more clear – Karan Shishoo Jan 21 '20 at 04:29
  • You wrote NAND (`not (a and b)`), but you're describing NOR (return true when both a and b are false). – wjandrea Jan 23 '20 at 01:39