# Booleans

Copied!
Happy Pythoning!

Brandon Hopkins

In the transcript, you state that if b: is shorthand for if b == True, but what happens when I state that b = “Hello”, Instead of a 0 or 1?

Well, the result is “B is true!”

But, when I specify that if b == True:, which you stated is shorthand for if b:, then I get a result of B is false!

So is if b: really the same as if b == True?

Because that’s kind of confusing me?

If so, am I missing something here? Is this error due to the nature of strings, not integers?

Was just playing around.

rezendef

I dumped that on chatgpot and it came up with the following:

The first code block checks if the value of b is exactly equal to the boolean value True. Since b is a string containing the value “Hello” and not a boolean, the condition b== True evaluates to False. Therefore, the else block is executed and the output is “B is false!”.

The second code block checks if b is truthy, which means it evaluates to True in a boolean context. In Python, any non-empty string is considered truthy, so the condition if b: evaluates to True. Therefore, the if block is executed and the output is “B is true!”.

My guess is that you’d have to used `b== True` without the `if` statement.

Martin Breuss RP Team

@Brandon Hopkins and @rezendef good question and nice digging for answers :D

Truthy and Falsy in Comparison Operations

It’s actually a little tricky and I can see where the confusion popped up. I’ve changed the transcript to say instead:

this is shorthand for `if bool(b) == True:`

Which is more accurate. For truthiness comparisons, Python passes the value to `bool()`, which gives you the expected results:

``````>>> bool("Hello")
True
>>> bool("")
False
>>> bool(3.0)
True
>>> bool(0.0)
False
>>> bool(1)
True
>>> bool(0)
False
>>> bool([0])  # Non-empty list
True
>>> bool({})  # Empty dictionary
False
``````

Here you can see that all falsy values (empty strings, empty containers, the values `0` and `0.0`, etc.) evaluate to `False`.

On the other hand, truthy values evaluate to `True`.

Booleans are Integers

What’s a bit tricky when you compare your example to the one shown in the course by Darren, is that he uses the values `1` and `0` in the initial example. These are actually equal to (`==`) `True` and `False`, respectively:

``````>>> 1 == True
True
>>> 0 == False
True
``````

That’s due to the fact that Boolean values in Python are subclasses of integers:

``````>>> issubclass(bool, int)
True
>>> issubclass(True.__class__, int)
True
``````

For all practical purposes of using comparison operators, you can forget about that, though. The main take-away is that Python does the comparison operation not by running `"Hello" == True`, but instead by running `bool("Hello") == True`.

Hope that helps and clarifies it :)

rezendef

@Martin Breuss Thanks for the explanation :) On a side note, I have been going through your ‘Get Started With Django’ course, great content, very thorough & It has changed my outlook on bugs too!!

Martin Breuss RP Team

Yay, glad you like it 🪲🐛🐞 :)

to join the conversation.