Python is Keyword



The Python is operator used to test if the two variables refers to the same object in the memory. If the variables refers to the same memory location, than it returns True, else return False. It is a case-sensitive keyword.

The is keyword and the equality[==] operator are used for comparison, but they are used in different context. The is keyword checks whether two variables have the same identity.

Identity refers to the memory address of an object. We use id() function to find identity. The equality [==] operator checks whether the values of two variables are equal, apart from referring to the same object in the memory.

Syntax

Following is the syntax of the Python is keyword −

Variable1 is Variable2

Example

Following is an basic example of Python is keyword −

# Initializing variables
var1 = True
var2 = True
# Using is 
result_1 = var1 is var2
print("The is operation between",var1,"and",var2,":",result_1)

Output

Following is the output of the above code −

The is operation between True and True : True

Checking Variables Identity

When we use the is keyword between the variables it checks the identity's of the given variables and return True when both the given variables have same id's else, returns False.

Example

We defined three variables, var1, var2, and var3. Since var1 and var2 had the same IDs, the is operation resulted in True. However, the IDs of var2 and var3 were different, so the is operation resulted in False

# Initialized the variables
var1 = [1,2,3]
var2 = var1
var3 = [1,2,3]
# Printing the Id's of variables
print("var1 Address :",id(var1))
print("var2 Address :",id(var2))
print("var3 Address :",id(var3))
# is Operation
result_1 = var1 is var2
result_2 = var2 is var3
print("var1 is var2 :",result_1)
print("var2 is var3 :",result_2)

Output

Following is the output of the above code −

var1 Address : 2501954230592
var2 Address : 2501954230592
var3 Address : 2501954228608
var1 is var2 : True
var2 is var3 : False

Using the 'is' Keyword with Tuple

The is keyword can also be used in Tuple. When the two different Tuples have the same object in the memory than the is keyword returns True, else returns False.

Example

Here, we have created three tuples,Tup1, Tup2 and Tup3. The result of is operation between Tup1 and Tup2 is False before assigning Tup1 to Tup2. The Tup2 is assigned to Tup3 and again Tup1 is assigned to Tup2. Again when we performed is operation on Tup1 and Tup2 it resulted True. And the is operation on Tup2 and Tup3 resulted in False.

# Initialized tuples
Tup1 = (14,7,8)
Tup2 = (23,89,12)
# is operator
result_1 = Tup1 is Tup2
print("Tup1 is Tup2 :",result_1)
Tup3 = Tup2
Tup2 = Tup1
result_2 = Tup2 is Tup3
result_3 = Tup1 is Tup2
print("Tup1 is Tup2 :",result_3)
print("Tup2 is Tup3 :",result_2)

Output

Following is the output of the above code −

Tuple1 is Tuple2 : False
Tuple1 is Tuple2 : True
Tuple2 is Tuple3 : False

Using 'is' with Equality Operator

The is operator and equality[==] operator both are used for comparing of two variables. The only difference is that the is operation refers to the same memory location apart from the values, whereas the equality [==] refers to the values of the variables.

Example

In the following, we have defined two lists List1 and List2 with the same values, but when we assigned a two lists it would create a separate memory location so when we had performed is operations it resulted in False. When we performed a equality operation it just checked values of the given to lists and resulted in True

# Initialized lists
List1 = ["Python", "Java", "CSS"]
List2 = ["Python", "Java", "CSS"]
# is operator
result_1 = List1 is List2
# Equality operator
result_2 = List1==List2
print("List1 is List2 :",result_1)
print("List1 == List2 :",result_2)

Output

Following is the output of the above code −

List1 is List2 : False
List1 == List2 : True

Using the 'is not' Operation

The is not operator checks whether no two variables has the same memory location. If the variables has different ID's it returns True, else it will return False.

Example

Following is an example of is not operation −

# Initialized variables
var1 = [1, 2, 3]
var2 = [1, 2, 3]
var3 = var1
# is not operation
result_1 = var1 is not var2
result_2 = var1 is not var3
print("var1 is not var2 :", result_1)  
print("var2 is not var2 :", result_2)

Output

Following is the output of the above code −

var1 is not var2 : True
var2 is not var2 : False
python_keywords.htm
Advertisements