7

I just stumbled across this and I couldn't find a sufficient answer:

x = ""

Why then is:

x == True
False

x == False
False

x != True
True

x != False
True

Am I supposed to conclude that x is neither True nor False?

5
  • What?! You are to conclude that x isn't equal to either True or False. Why did you think it would be? Have you been confused somehow by docs.python.org/2/library/stdtypes.html#truth-value-testing? It will still evaluate false-y in a boolean context: if x:, bool(x), etc.. Commented Mar 15, 2016 at 14:00
  • 2
    Dr. Tautology, I think what you meant to test was if bool(x) == False. Username checks out, though. Commented Mar 15, 2016 at 14:08
  • Now see what happens with your tests if you set x=0. And then do the same thing with x=1. Commented Mar 15, 2016 at 14:29
  • 2
    @PM2Ring Ask one bad question on stackoverflow and it ruins your day.FML Commented Mar 15, 2016 at 15:07
  • Hey, it's not that bad. Sure, it's got a net zero score, but you won a few rep points from it, and you got some good answers. But yes, this is fairly basic info that isn't that hard to find in the official documentation. Commented Mar 15, 2016 at 23:33

5 Answers 5

7

to check whether x is True or False:

bool("")
> False

bool("x")
> True

for details on the semantics of is and == see this question

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

Comments

5

Am I supposed to conclude that x is neither True nor False?

That's right. x is neither True nor False, it is "". The differences start with the type:

>>> print(type(""), type("x"), type(True), type(False))
builtins.str, builtins.str, builtins.bool, builtins.bool

Python is a highly object oriented language. Hence, strings are objects. The nice thing with python is that they can have a boolean representation for if x: print("yes"), e. g.. For strings this representation is len(x)!=0.

4 Comments

This answer suggests that if objects are different types they can't be equal. But 1 == 1.0 even though they're int and float, respectively.
@Barmar: The answer directly addresses the specific example given in the question which is about booleans. I am sorry if this mislead to generality. However, it already outlines that all objects/types can have a different representation, for instance the boolean one thanks to the magic method __bool__(self). Furthermore, objects of arbitrary types can be compared w. r. t. equality (==) by the magic method __equal__(self, other).
Furthermore, we have to distinguish between the identity (is) and the equality (==). While 1 and 1.0 are equal (1 == 1.0), they will never have the same identity due to the different types.
is is a whole other deal. (1, 2) is (1, 2) can be true or false.
2

In python '==' tests for equality. The empty string is not equal to True, so the result of your comparison is False.

You can determine the 'truthiness' of the empty string by passing it to the bool function:

>>> x = ''
>>> bool(x)
False

Comments

1

In a Boolean context, null / empty strings are false (Falsy). If you use

testString = ""

if not testString:
    print("NULL String")
else:
    print(testString)

As snakecharmerb said, if you pass the string to the bool() function it will return True or False based

>>> testString = ""
>>> bool(testString)
    False

>>> testString = "Not an empty string"
>>> bool(testString)
    True

See this doc on Truth Value Testing to learn more about this:

Python 2:

https://docs.python.org/2/library/stdtypes.html#truth-value-testing

Python 3:

https://docs.python.org/3/library/stdtypes.html#truth-value-testing

Comments

0

x represents an empty string literal object. By itself it is not a boolean type value, but as the other answers suggest a "cast" to boolean is possible. When used in an if statement condition check expression, the expression will evaluate to "False-y" due to the string being empty.

However your comparison examples compare the object x to the singleton objects "True" and "False", which falls back to a "is" comparison. Since the objects are different, the compare evaluates to False for when checking equality.

(For details look at Returning NotImplemented from __eq__ )

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.