'is' vs '==' in Python – What's the Difference? (And When to Use Each)

preview_player
Показать описание

It's easy to get tripped up by Python's "is" and "==" operators for object comparison. In this video I'll explain the difference with a few easy to understand examples.

There's a difference in meaning between equal and identical. And this difference is important when you want to understand how Python's is and == comparison operators behave.

The == operator compares by checking for equality - the "is" operator, however, compares identities.

The difference between them breaks down to down to two short definitions:

An is expression evaluates to True if two variables point to the same (identical) object.

An == expression evaluates to True if the objects referred to by the variables are equal (have the same contents).

* * *

FREE Python Tutorials & News:
Рекомендации по теме
Комментарии
Автор

a = [1, 2, 3]
b = a

c = [1, 2, 3]

print(a==c)
print(a is c)

Output:
True
False

TheMultipower
Автор

"is" statement is a syntactic sugar for "id(a) == id(b)"

Creuilcreuil
Автор

I think it's easiest to explain is that if "something is something" it just points to the same address in the memory, while "something == something" gives the same result, but both of the "somethings" are occupying a different "memory slot".

behi
Автор

is : equal pointers, == : equal content

BUDA
Автор

And this "is" very good video, thanks !

mounirben
Автор

Thanks! Short examples are very handy.

mikeafter
Автор

Thanks, I already new this in my head but couldn't quite explain it but this helped understand exactly how it operates!

finfims
Автор

this is so awesome - thank you so much

yougugu
Автор

your video and sound quality is great ❤️

kunalsoni
Автор

What are the cases when it is needed to identify the same object (by "is")?

annazaitseva
Автор

@Daniel Bader, Good Initiative, this was really helpful.
Hope to see a lot more tips soon

tonyjames
Автор

quality of content!!!
appreciate your work. thanks alot.

salmanali
Автор

twin cats that's adorable Dan Bader!

allainegaming
Автор

Hey Man, you are doing really awesome job... Thanks for the info man. Cheers!!!(Hoping to get python tricks book)

santoshsingh
Автор

Hi Dan,
Love this series of videos . Thanks a lot!!!
I had a query though regarding is and == Consider the following program:
A = 500
B = 500
Print(a is b)
Result :
Using console mode -> False
Using python script -> True
On further digging found that in console mode u get True for all values till 256 and False otherwise. But in script mode u get True always. could you explain this please ?

AbhishekSingh-witw
Автор

I´m doing python dev for a long time and never user the "as" keywork. Is that a good trick ?
For example in Exception as e, i can then use the details of the exception. Without the "as" i cannot.. learn that yesterday.

ramstein
Автор

Great explanation!! Quick question for Dan: I realize you're using the python REPL, but how do you get those autocomplete features when you start typing? Thanks in advance!

matthewkane
Автор

hey dan, thanks for the video. What is the editor your using.

shrav
Автор

"It depends upon what the meaning of the word 'is' is"
- Bill Clinton

RaabStephan
Автор

I think the best illustration here is,
b[1] = 7
print(a)
print(b)
print(c)

Great videos, Dan.

tamiryehuda