Skip to content

Effortlessly Master Python Basics

[

Using the “not” Boolean Operator in Python

Python’s not operator allows you to invert the truth value of Boolean expressions and objects. You can use this operator in Boolean contexts, such as if statements and while loops. It also works in non-Boolean contexts, which allows you to invert the truth value of your variables.

Using the not operator effectively will help you write accurate negative Boolean expressions to control the flow of execution in your programs.

In this tutorial, you’ll learn:

  • How Python’s not operator works
  • How to use the not operator in Boolean and non-Boolean contexts
  • How to use the operator.not_() function to perform logical negation
  • How and when to avoid unnecessary negative logic in your code

You’ll also code a few practical examples that will allow you to better understand some of the primary use cases of the not operator and the best practices around its use. To get the most out of this tutorial, you should have some previous knowledge about Boolean logic, conditional statements, and while loops.

Working With Boolean Logic in Python

George Boole put together what is now known as Boolean algebra, which relies on true and false values. It also defines a set of Boolean operations: AND, OR, and NOT. These Boolean values and operators are helpful in programming because they help you decide the course of action in your programs.

In Python, the Boolean type, bool, is a subclass of int. This type has two possible values, True and False, which are built-in constants in Python and must be capitalized. Internally, Python implements them as integer numbers:

>>> issubclass(bool, int)
True
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> isinstance(True, int)
True
>>> isinstance(False, int)
True
>>> int(True)
1
>>> int(False)
0

Python internally implements its Boolean values as 1 for True and 0 for False.

Getting Started With Python’s not Operator

The not operator allows you to invert the truth value of Boolean expressions and objects. It returns the opposite of the input value. If the input is True, the not operator will return False, and vice versa.

Here’s an example:

>>> not True
False
>>> not False
True

You can also use the not operator with variables:

>>> x = True
>>> not x
False

Using the not Operator in Boolean Contexts

The not operator is most commonly used in Boolean contexts, such as if statements and while loops, to evaluate whether a condition is True or False.

if Statements

In an if statement, you can use the not operator to check if a condition is False. If the condition is True, the code block inside the if statement will not be executed. Here’s an example:

x = 10
if not x > 5:
print("x is not greater than 5")

In this example, the condition x > 5 is True because x is 10. However, the not operator inverts the truth value of the condition, making it False. Therefore, the code block inside the if statement is not executed.

while Loops

In a while loop, you can use the not operator to determine when to exit the loop. The loop will continue executing as long as the condition is True. Here’s an example:

x = 0
while not x > 5:
print(x)
x += 1

In this example, the while loop continues executing as long as the condition not x > 5 is True. Since x starts at 0 and increments by 1 in each iteration, the loop will execute five times. Once x reaches 5, the condition becomes False, and the loop ends.

Using the not Operator in Non-Boolean Contexts

The not operator can also be used in non-Boolean contexts, allowing you to invert the truth value of non-Boolean variables. In these cases, the not operator will interpret the truth value based on the rules of truthiness and falsiness.

The truthiness of an object refers to its evaluation as True in a Boolean context, while falsiness refers to its evaluation as False.

Here’s an example:

x = 0
if not x:
print("x is falsy")

In this example, the not operator checks the truthiness of x. Since x is 0, which is considered falsy in Python, the condition not x evaluates to True. Therefore, the code block inside the if statement is executed.

Using the Function-Based not Operator

In addition to the unary not operator, Python provides the operator.not_() function in the operator module to perform logical negation. This function takes a single argument and returns the inverted truth value of that argument.

Here’s an example:

import operator
x = True
result = operator.not_(x)
print(result)

In this example, the not_() function is called with x as an argument. It returns the inverted truth value of x, which is False. The result is then printed to the console.

Working With Python’s not Operator: Best Practices

When using the not operator in your code, there are a few best practices to consider. These practices will help you write clean and readable code:

Test for Membership

When checking for membership in a sequence or collection, it’s generally recommended to use the not in operator instead of the not operator. This makes the code more explicit and easier to understand:

my_list = [1, 2, 3]
if 4 not in my_list:
print("4 is not in my_list")

In this example, the not in operator checks if 4 is not a member of the my_list list. If the condition evaluates to True, the code block inside the if statement is executed.

Check the Identity of Objects

When checking the identity of objects, use the is not operator instead of the not operator to distinguish between two different objects:

x = [1, 2, 3]
y = [1, 2, 3]
if x is not y:
print("x and y are different objects")

In this example, the is not operator checks if x and y are not the same object. Although the contents of x and y are the same, they are different objects with different memory addresses. Therefore, the condition evaluates to True, and the code block is executed.

Avoid Unnecessary Negative Logic

Avoid using unnecessary negative logic in your code, as it can make the code harder to read and understand. Instead, try to express conditions positively whenever possible:

age = 30
if age >= 18:
print("You are an adult")

In this example, the positive condition age >= 18 is easier to understand than the negative condition not age < 18. It also makes the code more readable and less prone to errors.

Conclusion

The not operator in Python allows you to invert the truth value of Boolean expressions and objects. It’s commonly used in Boolean contexts, such as if statements and while loops, to control the flow of execution in your programs. It can also be used in non-Boolean contexts to invert the truth value of variables.

By understanding how to effectively use the not operator and following best practices, you can write clean and readable code that accurately expresses your intentions.