top of page
  • Writer's pictureRevanth Reddy Tondapu

Part 30: Understanding the Difference Between is and == in Python


Difference Between is and == in Python
Difference Between is and == in Python

Hey there, young coders! Today, we’re going to explore two important concepts in Python: the is keyword and the == operator. Even though they seem similar, they do different things. By the end of this post, you'll know when to use each one and why they’re important. So, let's get started!


What Do is and == Do?

== Operator

The == operator checks if the values of two objects are equal. It compares the contents of the objects.

is Keyword

The is keyword checks if two variables point to the same object in memory. It compares the memory locations of the objects.


Let's See Some Examples

Example 1: Using == Operator

First, let’s create two lists with the same elements.

list1 = ["Revanth", "Coder", "Developer"]
list2 = ["Revanth", "Coder", "Developer"]

# Using the == operator
print(list1 == list2)  # Output: True

Here, the == operator checks if the contents of list1 and list2 are the same. Since both lists contain the same elements, it returns True.


Example 2: Using is Keyword

Now, let’s use the is keyword to compare the two lists.

print(list1 is list2)  # Output: False

Even though list1 and list2 have the same contents, they are stored in different memory locations. Therefore, list1 is list2 returns False.


Example 3: Making is Return True

Let’s see a scenario where the is keyword returns True.

list1 = ["Revanth", "Coder", "Developer"]
list2 = list1

# Using the is keyword
print(list1 is list2)  # Output: True

Here, we assigned list1 to list2. This means both list1 and list2 point to the same memory location. Therefore, list1 is list2 returns True.


Understanding Memory Locations

When you create a new list or any other object, Python stores it in a unique memory location. If you copy one list to another like we did in Example 3, both lists point to the same memory location.


Example 4: Changing Values

Let’s see what happens when we change an element in one of the lists.

list1 = ["Revanth", "Coder", "Developer"]
list2 = list1

# Changing an element in list2
list2[0] = "NewCoder"

print(list1)  # Output: ["NewCoder", "Coder", "Developer"]
print(list2)  # Output: ["NewCoder", "Coder", "Developer"]

Since list1 and list2 point to the same memory location, changing an element in list2 also changes it in list1.


Recap: is vs ==

  • == Operator: Checks if the values are the same.

  • is Keyword: Checks if the objects are stored in the same memory location.


Mutability and Memory

Lists are mutable, meaning you can change their contents. This affects how the is keyword behaves. If two lists are assigned to each other, they share the same memory location. Any change in one list reflects in the other.


Example 5: Preventing Changes with .copy()

To avoid unwanted changes, you can use the .copy() method to create a new list with the same contents but in a different memory location.

list1 = ["Revanth", "Coder", "Developer"]
list2 = list1.copy()

# Changing an element in list2
list2[0] = "NewCoder"

print(list1)  # Output: ["Revanth", "Coder", "Developer"]
print(list2)  # Output: ["NewCoder", "Coder", "Developer"]

Now, changing an element in list2 does not affect list1 because they are in different memory locations.


Conclusion

Understanding the difference between the is keyword and the == operator is crucial for writing correct and efficient Python code. Use the == operator to compare values and the is keyword to check if two variables point to the same memory location.

I hope you enjoyed this tutorial and found it helpful. Keep practicing these concepts, and soon you’ll be a Python pro! Stay tuned for more exciting tutorials and happy coding!

See you in the next post, and have a great day!

5 views0 comments

Comments


bottom of page