0

If None and None returns None, (None and None is None) should return True, No?

Was debugging an app and noticed that it returns None

3
  • 1
    Your assumptions about operator precedence are wrong. Commented Nov 11, 2022 at 15:10
  • is happens before and for the same reason that * happens before +. All operators in Python have precedence, not just the usual PEMDAS you learn about in early mathematics. Commented Nov 11, 2022 at 15:24
  • I skipped these (or don't remember) when I was learning lol. Makes sense now. Commented Nov 11, 2022 at 15:26

1 Answer 1

3

Good question!

(None and None is None)

"is" has a priority, so None is None returns True

"and" executed after that, so None and True result in None

If you add parenthesis, you can make it work in another way: ((None and None) is None) returns True

You can find more info on that topic here: https://docs.python.org/3/reference/expressions.html#operator-precedence

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.