Home » Python

is keyword with example in Python

Python is keyword: Here, we are going to learn about the is keyword with example.
Submitted by IncludeHelp, on April 15, 2019

Python is keyword

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 of is keyword

if (object1 is object2):
	statement(s)

Example:

    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

Python examples of is keyword

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	


Comments and Discussions!

Load comments ↻





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