Home »
Python
Python is Keyword
By IncludeHelp Last updated : December 07, 2024
Description and Usage
The is is a keyword (case-sensitive) in python, it is used to check whether two objects are the same objects or not.
Note: If two variables refer to the same objects then they are the same objects else objects are not the same objects even they contain the same value.
Syntax
Syntax of is keyword:
if (object1 is object2):
statement(s)
Sample Input/Output
Input:
# create a list
list1 = [10, 20, 30, 40, 50]
# creating list2 with list1
list2 = list1
# condition
if (list1 is list2):
print("Yes")
else:
print("No")
Output:
Yes
Example 1
Use of is keyword with different objects having same values.
# python code to demonstrate example of
# is keyword
list1 = [10, 20, 30, 40, 50]
list2 = [10, 20, 30, 40, 50]
# checking
if (list1 is list2):
print("list1 and list2 are the same objects")
else:
print("list1 and list2 are not the same objects")
Output
list1 and list2 are not the same objects
Example 2
Use of is keyword with the same objects.
# python code to demonstrate example of
# is keyword
# create a list
list1 = [10, 20, 30, 40, 50]
# creating list2 with list1
list2 = list1
# checking
if (list1 is list2):
print("list1 and list2 are the same objects")
else:
print("list1 and list2 are not the same objects")
Output
list1 and list2 are the same objects