Open In App

is keyword in Python

Last Updated : 14 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In programming, a keyword is a “reserved word” by the language that conveys special meaning to the interpreter. It may be a command or a parameter. Keywords cannot be used as a variable name in the program snippet. Python language also reserves some of the keywords that convey special meaning. In Python, keywords are case-sensitive.

Python is Keyword

The “is” keyword in Python is used to test object identity. The “is keyword” is used to test whether two variables belong to the same object. The test will return True if the two objects are the same else it will return False even if the two objects are 100% equal.

Note: The == relational operator is used to test if two objects are the same.

How is the ‘is’ keyword implemented in Python

The is a keyword in Python has the following syntax:

a is b
# Using if statement
if a is b:
statement(s)

Python is Keyword Usage

Let us see a few examples to know how the “is” keyword works in Python.

Compare List Elements using is Keyword

In this example, we will declare two different Python lists of the same elements and compare them using the ‘is’ keyword and the ‘==’ operator.

Python3




# Python program to demonstrate
# is keyword
 
x = ["a", "b", "c", "d"]
 
y = ["a", "b", "c", "d"]
 
print(x is y)
print(x == y)


Output :

False 
True

Python ‘is’ Keyword in For Loop Expression

In this example, we will declare two different objects with the same integer value and then use the ‘is’ keyword with Python if else statement.

Python3




# Python program to demonstrate
# is keyword
 
x = 10
y = 10
 
if x is y:
    print(True)
else:
    print(False)


Output:

True


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads