Difference for string comparison in Python: 'is' vs. ==

+7 votes

I noticed a Python script I was writing was acting squirrelly, and traced it to an infinite loop, where the loop condition was while line is not ''. Running through it in the debugger, it turned out that line was in fact ''. When I changed it to !='' rather than is not '', it worked fine.

Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values? I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.

Aug 23, 2018 in Python by bug_seeker
• 15,360 points
489 views

8 answers to this question.

+2 votes
I would like to show a little example on how is and ==are involved in immutable types. Try that:
a = 19998989890
b = 19998989889 +1
>>> a is b
False
>>> a == b
True
is compares two objects in memory, == compares their values. For example, you can see that small integers are cached by Python:
c = 1
b = 1
>>> b is c
True
You should use == when comparing values and iswhen comparing identities. (Also, from an English point of view, "equals" is different from "is".
answered Aug 23, 2018 by Kalgi
• 42,540 points
+1 vote

For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True.

Not always. NaN is a counterexample. But usually, identity (is) implies equality (==). The converse is not true: Two distinct objects can have the same value.

Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values?

You use == when comparing values and is when comparing identities.

When comparing ints (or immutable types in general), you pretty much always want the former. There's an optimization that allows small integers to be compared with is, but don't rely on it.

For boolean values, you shouldn't be doing comparisons at all. Instead of:

if x == True: # do something

write:

if x: # do something

For comparing against None, is None is preferred over == None.

I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.

Yes, that's exactly what it's for.

answered Aug 23, 2018 by Priyaj
• 56,940 points
+2 votes

In simple terms, the == expression is used to check whether the value of the objects are equal. The is expression is used to check whether the objects are pointing to the same objects/values. Let's try to understand this with an example.

>>> a = [1, 2, 3]
>>> b = a
>>> a
[1, 2, 3]
>>> b
[1, 2, 3]
>>> a == b
True
>>> a is b
True

Well, this is as expected because "a is equal to b" and "a is pointing to the same data as b."

>>> c = list(a)
>>> c
[1, 2, 3]
>>> a == c
True
>>> a is c
False


Now, you can see how it's different. Obviously "a is equal to c" because their values are same but "a is not pointing to the same data as c". a is pointing to [1,2,3] whereas c is pointing to list(a)

answered Aug 23, 2018 by Omkar
• 67,660 points
+2 votes

Difference between == and is operator in Python. 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. ... Hence list1 and list2 refer to different objects.
Hope this helps.

answered Aug 23, 2018 by P
• 190 points
+2 votes

The is expression is identity testing, == is equality testing. what happens in your code would be emulated in the interpreter like this:
>>> a = 'pub' 
>>> b = ''.join(['p', 'u', 'b']) 
>>> a == b 
True 
>>> a is b 
False
so, no wonder they're not the same, right?
In other words: is is the id(a) == id(b)

answered Aug 23, 2018 by digger
• 26,550 points

edited Aug 23, 2018 by digger
+4 votes
If we use "==" means both variables have same value. But if we use "is" it means both variables refer to same object.
If two variables belong to immutable data type. Then "==" and "is" works as same.
But if two variables belong to mutable data type then the result for "==" and "is" will be different. Because they will refer to different objects.
For example:
a = 10, b = 10, c = [1,2], d = [1, 2]

a == b
True
a is b
True
c == d
True
c is d
False
answered Sep 3, 2018 by Parul Raheja
+2 votes

‘Is’ use to check address of object , and == you can use to comparison of object value

answered Sep 20, 2018 by Kalpesh
+3 votes

'is' checks is it is the same object. meaning.. if you change a, you also change b and vice versa. '==' will check for equivalence. if you change a it won't necessarily change b or vice versa.

answered Sep 20, 2018 by Pieter van der Meer

edited Sep 20, 2018 by Vardhan

Related Questions In Python

0 votes
1 answer

Slice notation in Python for string reversal

The slice notation is [start:end:step]. Step = ...READ MORE

answered Apr 25, 2018 in Python by Nietzsche's daemon
• 4,260 points
99 views
+1 vote
2 answers

Difference between append vs. extend list methods in Python

Python append() method adds an element to ...READ MORE

answered Aug 21 in Python by germyrinn
• 180 points
259 views
0 votes
1 answer

What is the flow control for “continue” in python?

This is the way "continue" statement works! You ...READ MORE

answered Jul 16, 2018 in Python by Priyaj
• 56,940 points
58 views
+1 vote
1 answer

What is the correct order to learn concepts in Python for machine learning?

Machine Learning is a vast domain. It ...READ MORE

answered Jul 25, 2018 in Python by Anmol
• 3,620 points
77 views
0 votes
1 answer

Difference between append vs. extend list methods in Python

append: Appends object at the end. x = ...READ MORE

answered Aug 8, 2018 in Python by bug_seeker
• 15,360 points
147 views
+1 vote
1 answer

How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

answered Aug 20, 2018 in Python by Priyaj
• 56,940 points
4,344 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 350 views
+4 votes
6 answers