- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Python is Keyword
python is operator 100% 22200The is keyword in Python is used when we need to check whether two objects are same or not. For example:
a = {"Day": "Thu", "Month": "Dec"} x = a print(a is x) a = [10, 20, 30] x = a print(a is x)
The output is:
True True
Note - The is is used to check whether two variables in Python, refer to the same object or not. It returns True, if two variables refers to the same object. Otherwise returns False.
The is works similar to the == operator. The only difference is, == deals with values, whereas is deals with object.
Important - The is returns True, if specified objects are identical. Otherwise returns False
Since string and tuple are immutable, means value can not be changed when it is defined. Therefore any two equal strings are identical, because they refer to the same memory location. Similarly any two equal tuples are identical. For example, let's consider the following program:
x = "" y = "" print(x is y) x = () y = () print(x is y) x = tuple() y = tuple() print(x is y)
The output is:
True True True
And because list and dictionary objects are mutable, therefore:
x = [] y = [] print(x is y) x = list() y = list() print(x is y) x = dict() y = dict() print(x is y)
The output is:
False False False
The program given below is the last example program, demonstrating the is keyword in Python:
a = "fresherearth" b = "fresherearth" c = "Python Programming" print(a is b) print(a is c) a = (10, 20, 80) b = (10, 20, 80) c = (20, 30, 40) print(a is b) print(a is c) a = [1, 6, 9] b = [1, 6, 9] print(a is b) a = {"Day": "Thu", "Month": "Dec"} b = {"Day": "Thu", "Month": "Dec"} print(a is b)
The output is:
True False True False False False
« Previous Tutorial Next Tutorial »