The equality operator (==) compares the values ??of the two operands and checks if the values ??are equal. While the 'is' operator checks whether both operands refer to the same object or not (it is in the same place in memory).
# 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")
Output:
True False True False
list1 = [] list2 = [] print(id(list1)) print(id(list2))
Output:
139877155242696 139877155253640
This shows list1 and list2 refer to different objects.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.