Python IS NOT Operator

Python IS NOT

Python is not operation is simply the opposite of Python is.

Python is not checks if the two operands refer to the same memory reference. If they do not have the same memory reference, python is not returns True, else it returns False.

Example 1: Python IS NOT Operator

In the following example, we will use is not operator in the Python If statement.

Python Program

a = [5, 8]
b = [5, 8]
c = a

if a is not b:
	print('a is not b')
else:
	print('a is b')

if a is not c:
	print('a is not c')
else:
	print('a is c')
Run Code Copy

Output

a is not b
a is c

In the above example, the contents of a and b are same, but a and b are different objects having a different memory allocation.

But when a is assigned to c, only the reference is copied to c, but not the entire contents. So, if you change the contents of c, then the contents of a also would be changed, because they refer to the same list in memory. Hence a is not c returned False while a is not b returned True.

Summary

In this tutorial of Python Examples, we learned how to use is-not in Python, with the help of example programs.

Related Tutorials

Code copied to clipboard successfully 👍