How do I check if something is not in a list? How do I check if a condition is not met? The answer to these questions lies in one word: the not keyword.
The not keyword inverts the value of Booleans. Booleans are a data type that can be either one of two values: True or False. They are used to control the logic in a program.
In this guide, we’re going to talk about what the not keyword is and how it works. We’ll walk through three examples of where this keyword is used. Let’s get started!
What is Boolean Logic?
Boolean logic is used to make comparisons and to control the flow of a program. It allows you to work with conditions that can be either True or False.
Python if statements depend on Boolean logic. If a statement is True, the
if statement will run; otherwise, an
else statement will run, or nothing will happen.
What is the not Keyword?
The not keyword changes the meaning of a Boolean expression. It inverts its value.
If a value is True, the
not keyword will make it False, and vice versa. This means that we can use the
not keyword to check whether a condition is not met.
The not keyword is one of Python’s logical operators, alongside
or. Read our article on Python logical operators to learn more about these types of operators.
Logical operators are only one type of Python operator. For instance, there are arithmetic operators and identity operators.
How to Use the Not Keyword
The not keyword has a number of uses. It can be used to:
- Invert the value of a Boolean
- Check if a condition is not met with an “if” statement.
- Check if a value is not inside a statement with an “in” statement.
Let’s walk through an example of each of these.
Invert a Boolean
The simplest use of the
not keyword inverts a Boolean. This will set the value of a Boolean to be the opposite of the value it has been assigned.
We’re building a program that tracks whether a customer is a loyalty card holder. This program should have a feature that sets the customer’s loyalty card status to inactive if they decide to cancel their membership.
Let’s start by declaring a variable to store whether the customer has a loyalty card:
loyalty_card = True
A customer has notified us that they want to cancel their loyalty card. Next, we can use the
not keyword to do this:
loyalty_card = not loyalty_card print(loyalty_card)
Our code returns: False. This code has successfully flipped the value of “loyalty_card”. If loyalty_card was False, it would turn to True. In this example, loyalty_card was True so it inverted to False.
If Not Python
The if not keywords check whether an expression returns False. That’s one of the great things about the
not keyword: it makes Python so easy to read.
If not means
if an item is not equal to.
We are going to build a tool that checks if a customer is not a loyalty card holder at a clothing store. First, let’s declare whether the customer has a loyalty card:
loyalty_card = True
Our customer is a loyalty card holder. If a customer is not a loyalty card holder, they are not eligible for a discount; otherwise, they should receive a 5% discount on every purchase. We could use this code to check if a customer is eligible for a discount:
if not loyalty_card: print("This customer is not eligible for a discount.") else: print("This customer is eligible for a 5% discount.")
The “if not loyalty_card” checks if the value of “loyalty_card” is False. In this example, loyalty_card is True. This means that our expression evaluates to False and the contents of our
else statement are executed. This prints the following message to the console:
This customer is eligible for a 5% discount.
Our customer is a loyalty card holder so they are eligible for a discount.
Not In Python
You can use the not in keywords to check if an item is not inside a list. Let’s take the following list as an example:
presidents = ["George Washington", "John Adams", "Thomas Jefferson"]
This is a list of the first three Presidents of the United States. We can use the not in keyword to check if a particular person was in this select group.
A friend has just asked whether Alexander Hamilton was one of the first three presidents. We can check this using the following code:
presidents = ["George Washington", "John Adams", "Thomas Jefferson"] if "Alexander Hamilton" not in presidents: print("Alexander Hamilton was not one of the first three presidents.") else: print("Alexander Hamilton was one of the first three presidents.")
This expression checks whether “Alexander Hamiltion” is not in our list of presidents. If he is not in the list, the contents of our
if statement are run; otherwise, the contents of our
else statement are run.
Alexander Hamiltion is not in our list so the
else statement runs and prints the following to the console:
Alexander Hamilton was not one of the first three presidents.
not keyword is used in Boolean logic to control the flow of a program. It can be used to invert the value of a Boolean, check if a value is not inside another value, or check if a value is not equal to another value.
Are you up for a challenge? Write a program that checks whether your favorite food is in a dictionary of foods. You can make up this dictionary of foods for yourself. To learn more about dictionaries, read our tutorial on Python dictionary values.
Now you’re ready to start using the not keyword like an expert Pythonista!