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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.