
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Advanced Concepts
- Python - Abstract Base Classes
- Python - Custom Exceptions
- Python - Higher Order Functions
- Python - Object Internals
- Python - Memory Management
- Python - Metaclasses
- Python - Metaprogramming with Metaclasses
- Python - Mocking and Stubbing
- Python - Monkey Patching
- Python - Signal Handling
- Python - Type Hints
- Python - Automation Tutorial
- Python - Humanize Package
- Python - Context Managers
- Python - Coroutines
- Python - Descriptors
- Python - Diagnosing and Fixing Memory Leaks
- Python - Immutable Data Structures
- Python Useful Resources
- Python - Questions & Answers
- Python - Interview Questions & Answers
- Python - Online Quiz
- Python - Quick Guide
- Python - Reference
- Python - Cheatsheet
- Python - Projects
- Python - Useful Resources
- Python - Discussion
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
Python is Keyword
The Python is operator used to test if the two variables refers to the same object in the memory. If the variables refers to the same memory location, than it returns True, else return False. It is a case-sensitive keyword.
The is keyword and the equality[==] operator are used for comparison, but they are used in different context. The is keyword checks whether two variables have the same identity.
Identity refers to the memory address of an object. We use id() function to find identity. The equality [==] operator checks whether the values of two variables are equal, apart from referring to the same object in the memory.
Syntax
Following is the syntax of the Python is keyword −
Variable1 is Variable2
Example
Following is an basic example of Python is keyword −
# Initializing variables var1 = True var2 = True # Using is result_1 = var1 is var2 print("The is operation between",var1,"and",var2,":",result_1)
Output
Following is the output of the above code −
The is operation between True and True : True
Checking Variables Identity
When we use the is keyword between the variables it checks the identity's of the given variables and return True when both the given variables have same id's else, returns False.
Example
We defined three variables, var1, var2, and var3. Since var1 and var2 had the same IDs, the is operation resulted in True. However, the IDs of var2 and var3 were different, so the is operation resulted in False −
# Initialized the variables var1 = [1,2,3] var2 = var1 var3 = [1,2,3] # Printing the Id's of variables print("var1 Address :",id(var1)) print("var2 Address :",id(var2)) print("var3 Address :",id(var3)) # is Operation result_1 = var1 is var2 result_2 = var2 is var3 print("var1 is var2 :",result_1) print("var2 is var3 :",result_2)
Output
Following is the output of the above code −
var1 Address : 2501954230592 var2 Address : 2501954230592 var3 Address : 2501954228608 var1 is var2 : True var2 is var3 : False
Using the 'is' Keyword with Tuple
The is keyword can also be used in Tuple. When the two different Tuples have the same object in the memory than the is keyword returns True, else returns False.
Example
Here, we have created three tuples,Tup1, Tup2 and Tup3. The result of is operation between Tup1 and Tup2 is False before assigning Tup1 to Tup2. The Tup2 is assigned to Tup3 and again Tup1 is assigned to Tup2. Again when we performed is operation on Tup1 and Tup2 it resulted True. And the is operation on Tup2 and Tup3 resulted in False.
# Initialized tuples Tup1 = (14,7,8) Tup2 = (23,89,12) # is operator result_1 = Tup1 is Tup2 print("Tup1 is Tup2 :",result_1) Tup3 = Tup2 Tup2 = Tup1 result_2 = Tup2 is Tup3 result_3 = Tup1 is Tup2 print("Tup1 is Tup2 :",result_3) print("Tup2 is Tup3 :",result_2)
Output
Following is the output of the above code −
Tuple1 is Tuple2 : False Tuple1 is Tuple2 : True Tuple2 is Tuple3 : False
Using 'is' with Equality Operator
The is operator and equality[==] operator both are used for comparing of two variables. The only difference is that the is operation refers to the same memory location apart from the values, whereas the equality [==] refers to the values of the variables.
Example
In the following, we have defined two lists List1 and List2 with the same values, but when we assigned a two lists it would create a separate memory location so when we had performed is operations it resulted in False. When we performed a equality operation it just checked values of the given to lists and resulted in True −
# Initialized lists List1 = ["Python", "Java", "CSS"] List2 = ["Python", "Java", "CSS"] # is operator result_1 = List1 is List2 # Equality operator result_2 = List1==List2 print("List1 is List2 :",result_1) print("List1 == List2 :",result_2)
Output
Following is the output of the above code −
List1 is List2 : False List1 == List2 : True
Using the 'is not' Operation
The is not operator checks whether no two variables has the same memory location. If the variables has different ID's it returns True, else it will return False.
Example
Following is an example of is not operation −
# Initialized variables var1 = [1, 2, 3] var2 = [1, 2, 3] var3 = var1 # is not operation result_1 = var1 is not var2 result_2 = var1 is not var3 print("var1 is not var2 :", result_1) print("var2 is not var2 :", result_2)
Output
Following is the output of the above code −
var1 is not var2 : True var2 is not var2 : False