Skip to content
Advertisement

Why does “not(True) in [False, True]” return False?

If I do this:

JavaScript

That returns True. Simply because False is in the list.

But if I do:

JavaScript

That returns False. Whereas not(True) is equal to False:

JavaScript

Why?

Advertisement

Answer

Operator precedence 2.x, 3.x. The precedence of not is lower than that of in. So it is equivalent to:

JavaScript

This is what you want:

JavaScript

As @Ben points out: It’s recommended to never write not(True), prefer not True. The former makes it look like a function call, while not is an operator, not a function.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement