Table of contents
Introduction
Welcome to my 54th blog post on the Python journey. On day 54, I learned about the comparison operator 'is' and '=='. We will also have a look at the difference between the 2 operators in this blog post. Let's dive into more details and understand these higher-order functions in Python.
So let's get started......
'is' vs '==' in Python
'is' and '==' are both comparison operators that can be used to check if two values are equal.
'is' - compares the exact location in memory. Here we refer to the same object in memory. ''==" - compares values i.e the objects are the same or not
So the 'is' operator compares the identity of two objects, while the '==' operator compares the values of the objects. This means that 'is' will only return 'True' if the objects being compared are the exact same object in memory, while '==' will return True if the objects have the same value.
Example -
a = [1, 2, 3] #list
b = [1, 2, 3]
print(a == b) # True
print(a is b) # False
In the above example, a and b are two separate lists that have the same values, so \== returns True. However, a and b are not the same object in memory, so 'is' returns False.
Important Note - When we make constants in Python they are made only once. because Python knows that constants do not change, they are immutable and hence python does not waste more memory.
In the above example, lists are mutable and hence can change value.
So in Python, strings and integers are immutable, which means that once they are created, their value cannot be changed. This means that, for strings and integers, is and == will always return the same result:
Example -
a = "hello"
b = "hello"
print(a == b) # True
print(a is b) # True
a = 5
b = 5
print(a == b) # True
print(a is b) # True
In these cases, a and b are both pointing to the same object in memory, so 'is' and '==' both return True.
Finally in the case of mutable objects such as lists and dictionaries, is and == can behave differently. In general, you should use '==' when you want to compare the values of two objects and use 'is' when you want to check if two objects are the same object in memory.
Resources Used
You can watch the video of Day#54 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 54, I learned about the difference between the 'is' and '==' operators in Python. The major difference is, you should use '==' when you want to compare the values of two objects and use 'is' when you want to check if two objects are the same object in memory.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #54
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.