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.

Python Booleans: Leveraging the Values of Truth (Overview)

Understanding how Python Boolean values behave is important to programming well in Python. The Python Boolean type is one of Python’s built-in data types. It’s used to represent the truth value of an expression. For example, the expression 1 <= 2 is True, while the expression 0 == 1 is False.

In this course, you’ll learn how to:

  • Use Python Booleans to write efficient and readable Python code
  • Manipulate Boolean values with Boolean operators
  • Convert Booleans to other types
  • Convert other types to Python Booleans
  • Use short-circuit evaluation

Download

Sample Code (.zip)

4.9 KB

Download

Course Slides (.pdf)

1.6 MB

00:00 Hi, welcome to this Real Python video course on Python Booleans. My name is Cesar, and I’m going to be your instructor for the course. Whether you’re a beginner programmer or a professional software developer, you know that most useful programs rely on conditional execution—in other words, the ability to make decisions and then take the appropriate action.

00:21 Let me ask you a few questions. Are your if statements still looking like this? For example, you’ll execute one function or another depending on whether the length of some list is greater than 0, or in other words, if the list is non-empty.

00:37 Or, as another example, if some variable that’s supposed to hold a string is not the empty string, then you’ll display one greeting, otherwise you’ll display another.

00:49 Or say you have the return value of a couple of functions and execution depends on the order of these values. So for example, if the return value of func_1() is less than the return value of func_2(), and the return value of func_2() is less than MAX_LENGTH, then you’ll do something, otherwise you’ll do something else.

01:10 All right, last question. Which of the following is valid Python syntax? Say, this jumbled mess of various conditional statements, so 1 less than 2 in the list [1, 2] and so on.

01:24 Or how about this awkward looking expression involving True and False values with mathematical operators?

01:32 Or, set birthday = get_birthday() or "Unknown"?

01:38 If you’re unsure which of these is valid Python syntax or how the first few if statements could be improved, then maybe you need to take a closer look at Python’s built-in Boolean data type and how to take advantage of Python’s implementation of the various comparison and Boolean operators.

01:55 Here’s just a couple of things that you’ll learn after taking this course. You’ll learn how to write more succinct conditional statements, which can really pay off because a lot of your code is all about making decisions.

02:08 You’ll learn how to leverage Python’s implementation of the Boolean and comparison operators to write more computationally efficient code, and in general, broaden your understanding of the truth value of built-in and user-defined objects. By the end of this course, you’ll realize that there’s much more to Python Booleans than just True and False. All right, here’s a quick rundown of what you’ll learn in this course.

02:31 We’ll start off by taking a look at the Python Boolean type. This is the main character in this course, and you’ll see what the relationship is to the integer class.

02:41 We’ll then start a sequence of three lessons on the main Boolean operators in Python. We’ll start off with the not operator and then move on to the and operator and then the or operator. We’ll then take a look at the various comparison operators and how you can chain these operators to write computationally efficient code. We’ll then talk about how Python decides when objects are considered True and False, and how you can build in Boolean testing into your own classes.

03:10 We’ll wrap things up with a summary. All right, let’s get to it!

Become a Member to join the conversation.