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




[ad_1]

https://dbader.org/python-tricks ► Write clean & Pythonic code and start using advanced features in your Python code

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).

Check out this tutorial if you want to drill deeper: https://dbader.org/blog/difference-between-is-and-equals-in-python

FREE COURSE – “5 Thoughts on Mastering Python” https://dbader.org/python-mastery
PYTHON TRICKS: THE BOOK https://dbader.org/pytricks-book

SUBSCRIBE TO THIS CHANNEL: https://dbader.org/youtube

* * *

► Python Developer MUGS, T-SHIRTS & MORE: https://nerdlettering.com

FREE Python Tutorials & News:
» Python Tutorials: https://dbader.org
» Python News on Twitter: https://twitter.com/@dbader_org
» Weekly Tips for Pythonistas: https://dbader.org/newsletter
» Subscribe to this channel: https://dbader.org/youtube

Source


[ad_2]

Comment List

  • Real Python
    December 3, 2020

    what ide is this?

  • Real Python
    December 3, 2020

    Hey, many thanks! I visited several websites on this topic before stumbling upon this video. They made everything even more confusing and you, on the other hand, explained it in a readily comprehensible manner.

    By the way, I think you resemble Benedict Cumberbatch
    🙂

  • Real Python
    December 3, 2020

    Good video but is waaaaaay longer than it needs to be

  • Real Python
    December 3, 2020

    Few minutes about a book. Lots of chat about cats. Crumbs.

  • Real Python
    December 3, 2020

    too much introl… get to the point!

  • Real Python
    December 3, 2020

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

  • Real Python
    December 3, 2020

    So what you are saying is, "a" is a cat, "b" is another name for that cat, but "c" . . . is just a copy cat (but not the same cat)?

  • Real Python
    December 3, 2020

    5:24 Doesn't "c = list(a)" create a deep copy of a? If c were a shallow copy of a wouldn't "c is a" evaluate to True, because they share the same reference in memory?

  • Real Python
    December 3, 2020

    Sound quality is not impressive.

  • Real Python
    December 3, 2020

    How're you getting outputs like that without using Print statement? I'm using Pycharm so kinda confused on that

  • Real Python
    December 3, 2020

    thanks for info but sometimes talking too much

  • Real Python
    December 3, 2020

    "is" is not "being identical", "is" means being the same object located at exacrlty the same address. Then, the copy of an object will be "identical" to the original, but they will not be "is"-truthy. And "==" simply means __eq__() method returns True for obj1.__eq__(obj2).

    I liked few of your videos, but errors like these killed the interest for the rest for me.

  • Real Python
    December 3, 2020

    If you know JavaScript this is same as the equality operator (==) and the strict equality operator (===). The latter check if A = B based on the value, the former check if A = B based on the value and the data type. JS “==“ is the same as pythons “==“ and JS “===“ is the same as pythons “is”. NOTE: I am not bashing the illustration given by the tutor. Mine was for people with JavaScript background, which I think will be understandable.

  • Real Python
    December 3, 2020

    this is so awesome – thank you so much

  • Real Python
    December 3, 2020

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

    Great videos, Dan.

  • Real Python
    December 3, 2020

    Too wordy, stopped listening

  • Real Python
    December 3, 2020

    if a is b returns true then everytime a == b will return true correct me if I am wrong. the reason is any changes to a will be reflected to b as both point to same object

  • Real Python
    December 3, 2020

    Thanks for video and good one there: "think about cats all day"

  • Real Python
    December 3, 2020

    a=5 b=5, why a is b true?

  • Real Python
    December 3, 2020

    what is the app you are using ? pycharm ??

  • Real Python
    December 3, 2020

    Thank you 😊😊🤗🤗

  • Real Python
    December 3, 2020

    Thanks! your video really helped😊 Nice Explanation 👍
    I subbed 😁

  • Real Python
    December 3, 2020

    "is" is by reference. "==" is by value. Simple.

  • Real Python
    December 3, 2020

    Essentially it's evaluating the Address of variables

  • Real Python
    December 3, 2020

    You are damn good teacher.

  • Real Python
    December 3, 2020

    video starts at 6:25 and ends at 6:45

  • Real Python
    December 3, 2020

    I can't stop thinking about cats…

  • Real Python
    December 3, 2020

    Still didn't understand anything.

  • Real Python
    December 3, 2020

    Thanks ❤️

  • Real Python
    December 3, 2020

    Why isnt the example snakes or pythons?

  • Real Python
    December 3, 2020

    Does it work with gerbils?

  • Real Python
    December 3, 2020

    ty

  • Real Python
    December 3, 2020

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

  • Real Python
    December 3, 2020

    bendict cucumber ?

  • Real Python
    December 3, 2020

    Let me challenge you with a comment and question: The cats analogy doesn't work to describe this property of computer science. Even if cat1 and cat2 are identical twins, then cat1!=cat2 and cat1 is not cat2. A better example is: if a=[1,2,3] and b=a then a==b and {a is b); and if c=[1,2,3], then c==a –> True, however, (c is a) –> False. This is an example where mathematics and computer science differ…

Write a comment