Common mistakes we make in Python and why we should avoid that part 2

This is part 2 of the series. If you are new, you can check part 1 .

Today we are going to discuss “. In Python, sometimes we use is and == operators interchangeably. It will work at times, but not always. Let me explain this with an example.

Before getting into the topic, we all should know something about a Python object. Every Python object has two things in common:

  • First, its memory reference — Memory id can be found using the id function.
  • Second, its value

What does ‘is’ operator do:

The is operator checks whether two objects are pointing to the same memory location.

What does ‘==’ operartor do:

The == operator checks if the value of the operator is the same.

Here in this example, both is and == return the same result as both of them are pointing to the same memory location.

Note: For small common strings and integers in the range -5 to 256 the memory location will be the same. Python does this for optimization. This is a big topic that can be discussed later on in an article.

What is the solution:

If you are checking equality, then always use the == operator. Use is operator only when you are checking if an object is the same as another like

happy coding !!

Originally published at



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store