How to use Logical and Bitwise not Operator in Python

Logical not and Bitwise not operation in Python Programming

Use Logical not operator in Python

Most of the programming languages like Java, C, C++, etc has the functionality of accepting Boolean data that can be used to determine true or false on a certain expression.  Like most of the language, Python has the Boolean values strictly True or 1 and False or 0.

To get either True or false, probably we use the comparison operator also known as the logical gate or logic gate. There are some options like Logical or operator (or, OR, ||), Logical and operator (and, AND, &&), Logical not operator (not, NOT, !), Logical xor operator, etc.

In this tutorial, we will work with the logical not operator. In Python, logical not operator is written by the “not” keyword. See the example code below;

#How to use Logical not operator in python Program
#Logical not operator
a = not True
b = not False

print (a)
print (b)


Sample Input Sample Output
No input False

Notice the above output. by using not operator, we have changed that True value to False and similarly we have changed the False value to True. This means, Logical not swap the value between True and False. Using the not operator you can change any True value to False and vise-versa False value to True.

Another use of not operator is, using the not operator in the conditional statement. For example, in general, the if statement executes when a conditions return True. But in case we want to execute the if when a condition is False. Here how can we do that;

#Logical not operator in condition
num = 7 #Set num to value 7
if (not(num == 9)):
    print("Num is equal to 7 (if part executed)")
    print("Num is not equal to 7 (else part executed)")


Sample Input Sample Output
No input Num is equal to 7 (if part executed)

In the above output, in general, the else part should be executed because of the if condition is false as num is not equal to 9. Still, the if part has been executed. It is because of the not operator. The not operator converted the False value to True, that’s why ultimately the if condition considered as true and executed the if part.

Use Bitwise not operator in Python

Particularly in Python, the Bitwise operator is represented as the ‘~’ sign.  This sign may vary from language to language. Let’s have a quick example of the Bitwise operator;

#How to use Bitwise not operator in python Program
#Bitwise not operator
c = ~True
d = ~False

print (c)
print (d)


Sample Input Sample Output
No input -2

Notice the above output. we have got -2 as the bitwise value for True and we have got -1 as the bitwise value for False. Because unlike not operator, the bitwise operator returns the complement of a number instead of the complement of boolean value.

For the above program, when we applied the bitwise not function to True value, then it returned the complement number of inverse 1 that is -2. Because True represents 1 in integer mode. Similarly,  for False or 0 bitwise returned -1, that is the complement number of the inverse of 0.

Posted in