Home »
Python
not keyword with example in Python
Python not keyword: Here, we are going to learn about the not keyword with example.
Submitted by IncludeHelp, on April 12, 2019
Python not keyword
not is a keyword (case-sensitive) in python, it is a logical operator actually, it is used with a single operand, it returns True – if the condition evaluated to False and returns False – if the condition evaluated to True.
In other words, we can say, if the condition is True – it returns False and if the condition is False – it returns True.
Truth table for "not" keyword/operator
Condition not(Condition)
True False
False True
Syntax of not keyword/operator:
not(condition)
Example:
Input:
a = 10
# conditions
print(not(a == 10))
print(not(a != 10))
Output:
False
True
Python examples of not operator
Example1: Take a number and test the conditions using not operator
# python code to demonstrate example of
# not keyword/operator
a = 10
# printing return values
print(not(a == 10))
print(not(a != 10))
print(not(a > 10))
print(not(a < 10))
Output
False
True
True
True
Example 2: Input any number and print its square, if number is not 0
# Input any number and print its square,
# if number is not 0
num = int(input("Enter a number: "))
# checking the condition and
# calculate square
if not(num==0):
square = num*num;
print("Square of ", num, " is = ", square)
else:
print("An invalid input.")
Output
First run:
Enter a number: 21
Square of 21 is = 441
Second run:
Enter a number: 0
An invalid input.