Booleans
00:01
Booleans. What is a Boolean? A Boolean is a variable which can take two values—it’s either True
or False
. In Python, False
is equivalent to 0
and True
is equivalent to any non-zero number. Understanding the True
and False
concept, particularly this truthiness, is key to writing Pythonic code which is more readable than it would be otherwise.
00:31
You’re going to see some examples of this later on in this section. As you’ll see, defining a Boolean is as simple as setting it to True
or False
. Here a
has been set to True
, and you can see that the type is <class 'bool'>
.
00:49
Of course, the other option is False
, and b
will be defined using that.
00:55
In addition to this, though, objects are often evaluated as being True
or False
depending on their value. Here, setting the variable c
to 1
means we can then evaluate it as a bool
using the bool()
function.
01:16
Here you can see that the bool(c)
is equated to True
, and a bool(0)
equates to False
. This is a key concept in Python, and we’ll look at this in the next section.
01:36
Here, d
is going to be set to the value of 5
and let’s see what happens when we track for equivalence. We’re going to see if d
is actually equal to 5
using the double equals (==
) for equivalence, rather than assignment with a single one (=
).
01:52
And we can see that that is True
. So internally, Python evaluates these equations as Booleans. If we check to see if d
is equal to 4
, you can see that is False
.
02:04
This is the foundation of the way that Python understands equations. Next, you’re going to see a program which illustrates this a little more clearly. With the variable b
being set to 0
, it’s then going to be checked as to whether it’s True
, using the term if b:
—this is shorthand for if bool(b) == True:
—then 'B is true!'
.
02:27
In all the other cases we will print that 'B is false!'
. At the moment it’s set to 0
, so we would expect it to be False
.
02:39
And we can see B is false!
because b
is set to 0
. Changing b
to 1
and running the program again shows that b
is True
.
02:53
We can check this further with False
, and you can see that b
clearly is False
as it holds the value of False
.
03:02
And finally, setting it to True
gives us a predictable result of B is true!
. But this concept extends further. An empty list is False
because it doesn’t have any content.
03:16
This would also apply for an empty dictionary, as we’ll see here with the curly brackets. But it’s important to remember this isn’t about the values contained in there, so if we have a list with a value of 0
in it, that is not False
, as it exists.
03:32 Truthiness is a key component to writing Pythonic code, and once you understand it, your code will be clearer and simpler to understand when you come back to it, which is a key part of writing good code.
03:44 Some editors such as PyCharm will inform you when you’re not making Pythonic comparison statements and suggest improvements for your code.
rezendef on May 14, 2023
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 on May 15, 2023
@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 on May 16, 2023
@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 on May 19, 2023
Yay, glad you like it 🪲🐛🐞 :)
Become a Member to join the conversation.
Brandon Hopkins on March 16, 2023
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.