The not
keyword in Python is a logical operator used to obtain the negation or opposite Boolean value of an operand.
- It is a unary operator, meaning it takes only one operand and returns its complementary Boolean value.
- For example, if
False
is given as an operand to not
, it returns True
, and vice versa
How to Use Not Operator in Python?
The not
operator is simple to use. It is placed before a Boolean expression or a variable to invert its value.
Example: Basic example of not operator with True. Here, we used “not” operator to change the true value to false which is the negation of True.
Python
Practical Applications
The possible practical applications of the “not” keyword are:
- This keyword is mostly used for altering the boolean value.
- It is used with an if statement. It is used to negate the condition in the if statement.
- The “not” keyword is also used with “in keyword“. It is used with the “in” keyword when we are searching for a specific value in a collection of data.
More Examples on Not Operator
Let’s look at some examples of not operator in Python codes, each example shows different use-cases of “not” operator.
Python “not” operator with Variables
Basic example of “not” operator with variable.
Python
Using the “not” Boolean Operator in Python with Specific condition
As basic property of the “not” keyword is that it is used to invert the truth value of the operand, lets explore how the “not” operator negates the final outcomes.
Here: “not” operator negates the truth value of each expression, flipping True to False and False to True, whether it’s applied to direct Boolean values ( #1 and #2), logical operations (#3 and #4), or comparison results (#5).
Python
# 1 Not with False boolean value
print(not False)
# 2 Not with true boolean value
print(not True)_
# 3 Not with result of and operation
print(not(True and False))
# 4 Not with result of or operation
print(not(True or False))
# 5 Not with result of compare operation
print(not (5 > 7))
OutputTrue
False
True
False
True
Using the Not Operator with different Value
Here: when using the “not” keyword with different data types, it considers non-empty values as True and empty values as False and negates them.
Python
s = "geek"
print(not s)
# Not with list boolean value
a = [1, 2, 3, 4]
print(not a)
# Not with dictionary
d = {"geek": "sam", "collage": "Mit"}
print(not d)
# Not with Empty String
es = ""
print(not es)
# Not with Empty list
el = []
print(not el)
# Not with Empty dictionary
ed = {}
print(not ed)
OutputFalse
False
False
True
True
True
Logical NOT operator with the list
Here in this example, we are using “not” operator with the list:
Python
a = [5, 10, 20, 59, 83]
if not a:
print("Inputted list is Empty")
else:
for i in a:
if not(i % 5):
if i not in (0, 10):
print(i,"is not in range")
else:
print(i, "in range")
else:
print(i,"is not multiple of 5")
Output5 is not in range
10 in range
20 is not in range
59 is not multiple of 5
83 is not multiple of 5
“not” keyword is used in different ways here
- first, it checks if the list “l” is empty then, inside the loop
- it verifies whether each number is a multiple of 5
- and finally, it checks if a number is not in the given range (0,10)
By using the not
operator effectively, you can write clearer and more efficient logic in your Python programs.
Similar Reads
Python Operators
In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /,
6 min read
Precedence and Associativity of Operators in Python
In Python, operators have different levels of precedence, which determine the order in which they are evaluated. When multiple operators are present in an expression, the ones with higher precedence are evaluated first. In the case of operators with the same precedence, their associativity comes int
4 min read
Python Arithmetic Operators
Python Arithmetic Operators
Python operators are fundamental for performing mathematical calculations. Arithmetic operators are symbols used to perform mathematical operations on numerical values. Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). OperatorDescriptionS
5 min read
Difference between / vs. // operator in Python
In Python, both / and // are used for division, but they behave quite differently. Let's dive into what they do and how they differ with simple examples. / Operator (True Division)The / operator performs true division.It always returns a floating-point number (even if the result is a whole number).I
2 min read
Python - Star or Asterisk operator ( * )
The asterisk (*) operator in Python is a versatile tool used in various contexts. It is commonly used for multiplication, unpacking iterables, defining variable-length arguments in functions, and more. Uses of the asterisk ( * ) operator in PythonMultiplicationIn Multiplication, we multiply two numb
3 min read
What does the Double Star operator mean in Python?
The ** (double star)operator in Python is used for exponentiation. It raises the number on the left to the power of the number on the right. For example: 2 ** 3 returns 8 (since 2³ = 8)It is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python and is also known as Power Operator. Pr
2 min read
Division Operators in Python
Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. There are two types of division operators: Float divisionInteger division( Floor division)When an in
5 min read
Modulo operator (%) in Python
Modulo operator (%) in Python gives the remainder when one number is divided by another. Python allows both integers and floats as operands, unlike some other languages. It follows the Euclidean division rule, meaning the remainder always has the same sign as the divisor. It is used in finding even/
4 min read