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.
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.
Examples of Not Operator
Let's look at some examples of not operator in Python codes, each example shows different use-cases of "not" operator.
Example 1: Python "not" operator with Variables
Basic example of "not" operator with variable.
Python
Explanation: The not operator negates the value of a, turning False into True.
Example 2: Using the "not" Boolean Operator in Python with Specific condition
This example shows various ways to use the not operator with different Boolean values and expressions.
Python
print(not False)
print(not True)_
print(not(True and False))
print(not(True or False))
print(not (5 > 7))
OutputTrue
False
True
False
True
Explanation: The not operator negates each Boolean expression. For example, not True becomes False and not(False and True) becomes True.
Example 3: Using the Not Operator with different Value
This example demonstrates the behavior of the not operator with different data types like strings, lists and dictionaries.
Python
s = "geek"
print(not s)
a = [1, 2, 3, 4]
print(not a)
d = {"geek": "sam", "collage": "Mit"}
print(not d)
es = ""
print(not es)
el = []
print(not el)
ed = {}
print(not ed)
OutputFalse
False
False
True
True
True
Explanation: The not operator returns False for non-empty values (strings, lists, dictionaries) and True for empty ones. For example, not "geek" is False and not "" is True.
Example 4: Logical NOT operator with the list
This example uses the not operator in a condition to check the properties of items in a 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
Explanation: The not operator is used to check if the list a is empty or if a number is a multiple of 5. If the list is empty, it prints "Inputted list is Empty". It also checks for numbers not in the range [0, 10].
Related Articles:
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
4 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).It
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 numbe
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.Prec
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
Python Logical Operators