×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

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	

Comments and Discussions!

Load comments ↻





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