7554425D-0054-440D-B95E-D2ABC13D62CD Created with sketchtool.
{body}
Your Saved Info Packages

View your saved Course or Program Packages containing pricing and detailed curriculum.

Speak with a Learning Advisor.

Have any questions? We'll call you.

Fill out the form below and a Learning Advisor will reach out at a time convenient for you.

Please pick a valid date and time between 9 AM and 8 PM eastern (Monday to Friday)

By clicking "Book a call," you accept our Terms and will also receive exclusive offers and updates about new courses, workshops and events.

Python Tutorial

Python Is Keyword

Ready to start your career in Data? Find out more about BrainStation's Python Course

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:

  • Use == sign for comparing whether the values are the same.
  • Use is keyword 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 stack and hence using == sign is a very appropriate choice.
  • Values like objects and arrays are actually stored in a different memory called heap and 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 stack.

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.

Set Password

You already have an account with BrainStation, but you still need to set up a password.