Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

The not Operator

To learn more about this operator, check out Using the “not” Boolean Operator in Python.

00:00 First of all, a Boolean operator takes as input one or more Booleans and returns a Boolean. It’s convenient to completely specify a Boolean operator using what’s called the truth table, and we’ll do so for the three Boolean operators that we’ll focus on. In the mathematical theory of Boolean algebra, the three operators not, and, and or play an important role.

00:25 The reason is because any Boolean operator can be written in some way as a function of these three. These three operators are implemented in Python as not, and, and or—all lowercase.

00:40 You’ll see later on that Python’s implementation of and and or makes it easy to write efficient and readable code.

00:50 All right, let’s start off with the not operator. The not operator is the only unary Boolean operator implemented in Python. Unary is just a fancy word meaning that the operator takes only one input. In Python, to apply the not operator on the input x, you simply type not x.

01:11 However, not can be applied to any object, not just Boolean data types. not always returns either True or False, depending on the Boolean value of the input. Now, I mention this because not is the only Boolean operator that we’ll discuss that always returns a Boolean data type.

01:30 The other two operators that we’ll talk about, and and or—they will return one of the values of the inputs, and we’ll discuss this in upcoming lessons and how you can take advantage of it.

01:43 Here is the truth table to the not operator. So, if the input x has a Boolean value of True, then not x returns False.

01:53 And if the input x has an input value of False, then not x returns True.

02:01 So not just simply negates the Boolean value of its input.

02:07 All right, let’s try out how the not operator works on just some basic objects. So, not True, as we saw, will return False. And not False will return True. We can also apply not to integers, so not 1 returns False.

02:25 A way to interpret this is that 1 has a truth value of True, and so not True returns False. Whereas not 0 returns True.

02:37 A way to interpret this is that 0 has a truth value of False, and so not False is why we get True.

02:46 Let’s apply not to a string. So, for example, not 'hello world'.

02:53 In this case, we get False. The idea is that this non-empty string has a True value, and so not True is False.

03:01 Whereas if you were to try not with the empty string, you’re going to get True. You can interpret this as meaning that the empty string has a Boolean value of False. Let’s try this, say, on a list.

03:15 Let’s say the list [1, 2, 3]. not of this list returns False. A way for you to interpret this is that this non-empty list has a Boolean value of True.

03:28 Whereas if you were to try not with the empty list, in this case, you get True. You interpret this as meaning that the empty list has a Boolean value of False.

03:41 Let’s take a look at a quick example of how you could use the not operator to set a default value for a string in case the string is empty.

03:50 So, for example, let’s suppose that you have a field in either a database or maybe the input in some form, and it’s supposed to contain the first name of a user, and the user didn’t pass in a first name and so first_name is an empty string.

04:08 You want to display something for that first_name, and what you could do is then set a default value that you want to display in case the first_name is an empty string.

04:20 You could do this using an if statement, if not first_name. In this case, if first_name is an empty string, it will have a Boolean value of False, and not False is True, so we’ll enter into the if block.

04:36 And then maybe what you want to do is set the first_name string to, say, something like "Not given". The idea is going to be that if first_name is an empty string, then you’re going to display somewhere that the first name wasn’t given.

04:50 Go ahead and run that code. In this case, because first_name is an empty string, the if condition is going to be True, and so first_name is given the value of "Not given".

05:03 On the other hand, if first_name does have a non-empty value—so, for example, "Luigi"—in this case, our if condition in the not first_name will return a value of False because a non-empty string has a Boolean value of True and not True returns False.

05:22 And so this part of the code won’t be run inside the if statement. So in this case, our first_name still has a value of 'Luigi'.

05:33 Now, you could do this a little bit faster using the ternary operator in Python. For example, in the case where you have an empty string, you could do something like first_name, we’ll set it equal to "Not given" if the user did not pass a first name.

05:50 And so in this case, we’ll say "Not given" if not first_name, otherwise first_name should just stay as the value that was passed in by the user, so we’ll return first_name. So in this case, because the string first_name was empty, it’s going to be assigned a value of "Not given".

06:12 Whereas in the case where the name was "Luigi",

06:18 in this case, when we use the ternary operator, the not first_name returns a value of False because in this case first_name is a non-empty string, and so this condition after the if statement is False.

06:32 And so the value that first_name is set to is just the value that it currently holds, which in this case was "Luigi". So if we run this code, first_name is still 'Luigi'. All right!

06:47 This ends this lesson on the not operator. In the next lesson, we’ll take a look at the and operator.

Become a Member to join the conversation.