What is the difference between == and is in Python?
The == operator checks if two values are equal. The is operator checks if two names point to the same object in memory.
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b)
print(a is b)
print(a is c)
Output:
Tutorials Class - Output Window
True
False
True
a and b have the same values but are two separate lists. c is just another name for the same list as a. Use == to compare values, and use is mainly for None, like if x is None:. See Python Operators.
Learn more about the similar topics:
| Tutorials |
|---|
| Python Variables |
| Python Data Types |
| Python Type Casting |
| Python Operators |
| Exercises & Assignments |
|---|
| Python program to add two numbers |
| Python program to swap two variables |
| Python program to find the area of a circle |
| Python program to convert Celsius to Fahrenheit |
| Interview Questions & Answers |
|---|
| What is the difference between == and is in Python? |