Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 6, 2019 12:00 am GMT

Python 'is' vs '=='

A lot of times when I'm doing ifs in Python, I find myself wondering whether to use is or == for the check.

# do I doif a is b:    ...# orif a == b:    ...

It can be a bit confusing if you're new to Python, and it's easy to assume the two can be used interchangeably. So, what's the difference?

is

The is operator checks if both elements point to the same object. Let's fire up a python console to help illustrate this:

$ python3Python 3.7.4[Clang 10.0.1 (clang-1001.0.46.4)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> a = []>>> b = []>>> c = a>>> a[]>>> b[]>>> c[]>>>

So, we've declared three variables and assigned them values. a and b are both empty lists, and c = a. We can see that all three variables contain an empty list. Using is to compare them:

>>> a is bFalse>>> b is cFalse>>> a is cTrue

Despite the fact that a and b seem identical (in that they're both empty lists), the variables a and b do not point to the same object, therefore a is b evaluates to False. The same goes for b is c.

Conversely, because we assigned the variable a to c, they both point to the same object, thus a is c is True.

==

== on the other hand checks if both elements contain equal values. Whether or not they point to the same object doesn't matter here.

>>> a == bTrue>>> b == cTrue>>> a == cTrue

All checks using == evaluate to True, because the values of a, b and c are all equal. If d = [1, 2, 3] is introduced, a == d, b == d and c == d would all be False, because the values are not equal.

So if you want to check that elements point to the same object, use is. If you're only interested in the equality of the values, use ==.


Original Link: https://dev.to/wangonya/python-is-vs-28eb

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To