‘is’ vs ‘==’ to Check if Two Elements are Equal in Python

Python objects can be compared using two operators: == and is.

Examples

Compare x to None using is:

if x is None:
print(‘Object x is None’)

Compare x to the empty string ” using ==:

if x == ”:
print(‘x is an empty stri…


This content originally appeared on DEV Community and was authored by Carmine Scarpitta

Python objects can be compared using two operators: == and is.

Examples

Compare x to None using is:

if x is None:
    print('Object x is None')

Compare x to the empty string '' using ==:

if x == '':
    print('x is an empty string')

Apparently the two operators is and == can be used interchangeably, but this is not the case.

Difference between == and is

== is a equality operator. It is used to check if two objects are equal or not.

is is an identity operator. It is used to check if two objects are actually the same object or not. In other words, it checks if the two objects share the same memory location.

When should we use 'is' and when '=='?

In general, if you are comparing an object to a sigleton like None, True or False you should always use is. There are some exceptions, but most of the time this is the case.

For all the other object types (e.g. strings and numbers), using is can lead to unexpected behavior. To compare these object types, you must always use ==.

Conclusions

Data Type Compare using
None is
bool is
str ==
int ==
float ==
list ==
tuple ==
dict ==
bytes ==
dict ==


This content originally appeared on DEV Community and was authored by Carmine Scarpitta


Print Share Comment Cite Upload Translate Updates
APA

Carmine Scarpitta | Sciencx (2022-04-12T22:50:52+00:00) ‘is’ vs ‘==’ to Check if Two Elements are Equal in Python. Retrieved from https://www.scien.cx/2022/04/12/is-vs-to-check-if-two-elements-are-equal-in-python/

MLA
" » ‘is’ vs ‘==’ to Check if Two Elements are Equal in Python." Carmine Scarpitta | Sciencx - Tuesday April 12, 2022, https://www.scien.cx/2022/04/12/is-vs-to-check-if-two-elements-are-equal-in-python/
HARVARD
Carmine Scarpitta | Sciencx Tuesday April 12, 2022 » ‘is’ vs ‘==’ to Check if Two Elements are Equal in Python., viewed ,<https://www.scien.cx/2022/04/12/is-vs-to-check-if-two-elements-are-equal-in-python/>
VANCOUVER
Carmine Scarpitta | Sciencx - » ‘is’ vs ‘==’ to Check if Two Elements are Equal in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/12/is-vs-to-check-if-two-elements-are-equal-in-python/
CHICAGO
" » ‘is’ vs ‘==’ to Check if Two Elements are Equal in Python." Carmine Scarpitta | Sciencx - Accessed . https://www.scien.cx/2022/04/12/is-vs-to-check-if-two-elements-are-equal-in-python/
IEEE
" » ‘is’ vs ‘==’ to Check if Two Elements are Equal in Python." Carmine Scarpitta | Sciencx [Online]. Available: https://www.scien.cx/2022/04/12/is-vs-to-check-if-two-elements-are-equal-in-python/. [Accessed: ]
rf:citation
» ‘is’ vs ‘==’ to Check if Two Elements are Equal in Python | Carmine Scarpitta | Sciencx | https://www.scien.cx/2022/04/12/is-vs-to-check-if-two-elements-are-equal-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.