The == operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands refer to the same object or not.
# python3 code to # illustrate the # difference between # == and is operator # [] is an empty list list1 = [] list2 = [] list3=list1 if (list1 == list2): print("True") else: print("False") if (list1 is list2): print("True") else: print("False") if (list1 is list3): print("True") else: print("False") list3 = list3 + list2 if (list1 is list3): print("True") else: print("False") |
chevron_right
filter_none
Output:
True False True False
- Output of the first if condition is “True” as both list1 and list2 are empty lists.
- Second if condition shows “False” because two empty lists are at different memory locations. Hence list1 and list2 refer to different objects. We can check it with id() function in python which returns the “identity” of an object.
- Output of the third if condition is “True” as both list1 and list3 are pointing to the same object.
- Output of the fourth if condition is “False” because concatenation of two list is always produce a new list.
list1 = [] list2 = [] print(id(list1)) print(id(list2)) |
chevron_right
filter_none
Output:
139877155242696 139877155253640
This shows list1 and list2 refers to different objects.
Recommended Posts:
- Benefits of Double Division Operator over Single Division Operator in Python
- Difference between 'and' and '&' in Python
- New '=' Operator in Python3.8 f-string
- Difference between continue and pass statements in Python
- Python - Difference between sorted() and sort()
- Difference between Yield and Return in Python
- Difference Between x = x + y and x += y in Python
- Difference between Method Overloading and Method Overriding in Python
- Difference between dir() and vars() in Python
- What is the difference between Python's Module, Package and Library?
- Python | Using variable outside and inside the class and method
- 10 Essential Python Tips And Tricks For Programmers
- Python | Float type and its methods
- How to assign values to variables in Python and other languages
- Namespaces and Scope in Python
- Statement, Indentation and Comment in Python
- Download and Install Python 3 Latest Version
- break, continue and pass in Python
- Primary and secondary prompt in Python
- Check the equality of integer division and math.floor() of Regular division in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : XAkash

