Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Booleans

Give Feedback

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:31 Booleans: Truthiness.

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 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.

Become a Member to join the conversation.