Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the difference between "==" and "is" operator in Python?

Saad Akbar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The is operator determines whether the two operands refer to an identical object or not. On the other hand, the == operator takes the operands’ values and compares them to check if they are equal.

Code

l1 = []
l2 = []
l3 = l1
if (l1 == l2):
print("True")
else:
print("False")
if (l1 is l2):
print("True")
else:
print("False")
if (l1 is l3):
print("True")
else:
print("False")
l3 = l2
if (l2 == l3):
print("True")
else:
print("False")

Explanation

  • The output of the first if condition is True as both l1 and l2 are empty.

  • The second if condition returns False as l1 and l2 point to different objects with the same value but different memory locations.

  • The third if condition returns True as equating l3 to l1 means both point to the same object.

  • The fourth if condition returns True as equating l3 to l2 means both point to the same object. This means they both have the same value as well.

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring