Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

If I do this:

>>> False in [False, True] True 

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

But if I do:

>>> not(True) in [False, True] False 

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

>>> not(True) False 

Why?

like image 882
Texom512 Avatar asked Jul 15 '15 04:07

Texom512


People also ask

What does true and false return?

The true operator returns the bool value true to indicate that its operand is definitely true. The false operator returns the bool value true to indicate that its operand is definitely false.

Does 1 equal True or false?

1 is considered to be true because it is non-zero. The fourth expression assigns a value of 0 to i. 0 is considered to be false.

Is 0 and 0 false or true?

Instead, comparison operators generate 0 or 1; 0 represents false and 1 represents true.


1 Answers

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

>>> not ((True) in [False, True]) False 

This is what you want:

>>> (not True) in [False, True] True 

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.

like image 176
Yu Hao Avatar answered Sep 23 '22 21:09

Yu Hao