Python Is Keyword
Oftentimes while writing conditional logic, developers have to compare for equality of two variables or values. Equality operator
== is always used for such comparisons. While
== is used for testing whether two variables are the same or not,
is keyword in Python will compare whether two values refer to the same object or not.
One key difference to note while deciding on whether to use
== operator or
is keyword is:
==sign for comparing whether the values are the same.
iskeyword for comparing objects equality. It is very useful when comparing whether non-primitive values like objects and arrays are equal or not.
- Values like strings, numbers, and booleans are stored in the same type of memory called
stackand hence using
==sign is a very appropriate choice.
- Values like objects and arrays are actually stored in a different memory called
heapand hence a direct comparison of objects and arrays that look identical will still end up being unequal.
Let’s explore the difference between
== operator and
is keyword in the code snippet below.
As seen above, comparing values that are not objects and arrays using
== operator and
is keyword results in the same outcome as values like strings and booleans are stored in the same memory type called
As seen above, comparing values that are objects and arrays and have the same values compares equal using
== operator as it only compares the values. However, when compared using the
is keyword, the values are compared to be referring to the same object or array and because the objects and arrays are stored separately in
heap, they are compared as unequal.
Kick-Start Your Data Career
We offer a wide variety of programs and courses built on adaptive curriculum and led by leading industry experts.
- Work on projects in a collaborative setting
- Take advantage of our flexible plans and scholarships
- Get access to VIP events and workshops
The Python certificate course provides individuals with fundamental Python programming skills to effectively work with data.
Taught by data professionals working in the industry, the part-time Data Science course is built on a project-based learning model, which allows students to use data analysis, modeling, Python programming, and more to solve real analytical problems.
The Data Science bootcamp is an intensive course designed to launch students' careers in data.
The part-time Data Analytics course was designed to introduce students to the fundamentals of data analysis.