×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python not Keyword

By IncludeHelp Last updated : December 07, 2024

Description and Usage

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

Truth table for "not" keyword/operator

Condition	not(Condition)
True		False
False 		True

Syntax

Syntax of not keyword/operator:

not(condition)

Sample Input/Output

Input:
a = 10

# conditions
print(not(a == 10))
print(not(a != 10))

Output:
False
True

Example 1

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.
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement



Copyright © 2024 www.includehelp.com. All rights reserved.