‘is’ vs ‘==’ Operators In Python. Topic – 54

‘is’ vs ‘==’ Operators In Python. Topic – 54

In Python programming, there are two more useful operators. The first one is ‘=='(double equals to) and another one is ‘is’ operator. We will learn how it works. Both are comparison operators but the working principle is different.

‘==’ (doubble equals to operator):

‘==’ operator use to compare the two values. This operator checks whether the two given values are same or not. Generally, it compares the values of the variable. Let’s understand this through a little story:

One day, Shivam & Priti, who read in the same school. They are celebrating the birthday of their common friends. Both had identical gift boxes.

# Identical gift boxes
shivam_gift = ["Toy", "Chocolate", "Balloon"]
priti_gift = ["Toy", "Chocolate", "Balloon"]

Shivam and Priti were excited to compare their gifts.

# Comparing the gifts using ==
if shivam_gift == priti_gift:
    print("Shivam and Priti have same gifts!")
else:
    print("Shivam and Priti have different gifts.")

When they compared the gift box through the ‘==’ operators, they found out that both of their gifts were the same. It’s because ‘==’ operator compares the values of the elements.

From this small story, we can understand that ‘==’ operator compares the values, Like in the case of both gifts. Now see an example:

a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)

The Output Of This Code Will Be:

Output
# Output: True

‘is’ (Identity Operator):

The ‘is’ operator checks whether two variables refer to the same location of the memory or not. The ‘is’ operator doesn’t compare the values, only checks, whether two variables point to the same objects or not. Let us now understand the ‘is’ operator through a simple story.

One day Shivam & Priti create a diary and write some notes. They started using the same diary.

# Diary creation
shivam_diary = ["Meeting at 10 AM", "Write python coding", "Read the algorithm"]
priti_diary = ["Make a javascript project", "Read about Epress js."]

# Shared diary
shared_diary = Shivam_diary 

Now, both of them decided that if one person changes anything in the diary, then the other person should also see the changes. That’s why they decided to point to the same diary.

# Shared diary after deciding to share
shivam_diary = shared_diary
priti_diary = shared_diary

Now, time to change something. Shivam decided to add some new notes to the diary.

# Shivam adds a new task
shivam_diary.append("Create a website for client")

Priti, who is also the point of the shared diary, thinks to see what has changed.

# Priti checks the shared diary
print(priti_diary)

Surprisingly, when Priti checked the dairy she saw a new task added in dairy. “Create a website for client” a task that was added by Shivam, Priti also saw the same thing. This happened because both were pointing to the same object.

There are ‘is’ operator is used. When we do ‘Priti_diary is shared_diary’ it was checked whether both the variables were pointing to the same object or not. If both are pointed to the same objects then whatever changes happen in one variable will be reflected in the other variable as well.

# Using is operator to check if both point to the same object
print(priti_diary is shared_diary)  

The Output Of This Code Will Be:

Output
# Output: True

This story explains how this operator helps in checking object identity where both variables are pointing to the same object. Now see a proper example:

x = [1, 2, 3]
y = [1, 2, 3]
z = x

print(x is y)  
print(x is z) 

The Output Of This Code Will Be:

Output
True
False

‘is’ vs ‘==’ Operators In Python. Topic – 54

some practice sets:

`is` Operator Questions:

Practice Set
#1. Question 1:
   x = [1, 2, 3]
   y = x
 # Check whether x and y point to the same object.

Practice Set
#Question 2:
   a = 10
   b = a
 # Check whether a and b point to the same object.

Practice Set
#Question 3:
   list1 = [1, 2, 3]
   list2 = [1, 2, 3]
# Check whether list1 and list2 point to the same object.

Practice Set
#Question 4:
   name1 = "Alice"
   name2 = "Alice"
# Check whether name1 and name2 point to the same object.

Practice Set
#Question 5:
   dict1 = {'a': 1, 'b': 2}
   dict2 = dict1
# Check whether dict1 and dict2 point to the same object.

`==` Operator Questions:

Practice Set
#Question 6:
   arr1 = [4, 5, 6]
   arr2 = [4, 5, 6]
# Check whether arr1 and arr2 have the same values.

Practice Set
#Question 7:
   num1 = 20
   num2 = 20
# Check whether num1 and num2 have the same values.

Practice Set
#Question 8:
   fruits1 = ['apple', 'orange', 'banana']
   fruits2 = ['apple', 'orange', 'banana']
# Check whether fruits1 and fruits2 have the same values.

Practice Set
#Question 9:
   word1 = "hello"
   word2 = "world"
# Check whether word1 and word2 have the same values.

Practice Set
#Question 10:
   set1 = {1, 2, 3}
   set2 = {3, 2, 1}
# Check whether set1 and set2 have the same values.

Try solving these questions and if you have any doubts or need explanations, feel free to ask!

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

Your email address will not be published. Required fields are marked *

ABOUT ME
Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog