Python Quick Tip: The Difference Between "==" and "is" (Equality vs Identity)
8:51

Python Quick Tip: The Difference Between "==" and "is" (Equality vs Identity)

Corey Schafer 06.08.2019 126 956 просмотров 4 367 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
In this Python Programming Tutorial, we will be learning the difference between using "==" and the "is" keyword when doing comparisons. The difference between these is that "==" checks to see if values are equal, and the "is" keyword checks their identity, which means it's going to check if the values are identical in terms of being the same object in memory. We'll learn more in the video. Let's get started... Mutable vs Immutable - https://youtu.be/5qQQ3yzbKp8 ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Python

Оглавление (2 сегментов)

Segment 1 (00:00 - 05:00)

hey there how's it going everybody in this video we're just going to go over a quick tip and discuss a question that I get from time to time so what we're going to be looking at is the difference between the double equals and the is keyword when doing comparisons so the difference between these is that the double equals checks for equality checks if values are equal and the is keyword checks their identity which means it's going to check if the values are identical in terms of being the same object in memory so first let me give an example using real-world objects to try to explain this concept and then we'll take a look at some code examples to solidify those points so let's say that I wanted to compare soft drinks at a party so let's pretend that my brother and I both have a drink so he has a can of coke and I have a can of Pepsi so let me write these down here now try to think of these as actual objects in the real world and not Python strings for the moment just so we can conceptualize this so if he had a can of coke and I Pepsi if we compared those for equality using the double equals comparison then those wouldn't be equal because he has Coke and I have a Pepsi those aren't the same but now let's say that my brother and I both have cans of Pepsi so if we compared those using the double equals comparison then we could consider those equal since they're both cans of Pepsi and have the same ingredients and things like that but ultimately equality can be a little subjective it just depends on the objects that you are comparing and kind of what the developer considers equal so for example if you know maybe it's good enough if these are the same types of soft drinks that we could consider those equal but maybe you know just depending on how we set up our program these would only be equal if they were both Pepsi and they were both in blue cups or something like that so equality can be a little subjective and we'll take another look at an example of this once we actually look at some code okay but now let's see if we were to compare our drinks using the keyword instead then this wouldn't be true because the is keyword checks if they are actually the same object so in this example we have two different cans of Pepsi and even though we could consider those equal since they're the same type of drink there's still different objects so for example if I was to put a lemon in my drink then my brother's drink would still stay the same it wouldn't have a lemon in it now I could change that real-world example and instead say that my brother and I are going to share the same cane of Pepsi so we can describe that in multiple ways so this would be like having multiple variables that point to the same object so I could say you know Cory's drink is a can of Pepsi and I could also say that my brother's drink is that same can of Pepsi so now if we were to check if me and my brother's drinks for equality using the double equals comparison then that would be true because they're the same object but if we compared the drinks using the is keyword then that would also be true because we're sharing that exact same object and since we're sharing that single can of Pepsi then if I were to put a lemon in my drink then my brother would also have a lemon in his so that's a quick overview of the difference using a real-world example but now let's see what this looks like in code and it'll probably make more sense and if that example didn't make sense to you then this should clear it up so let me create two different lists here so I'm going to create l1 and I'm just going to set this equal to one two three four five and then I'll also create an l2 but I will take out a value here so just one two four five and now I'm going to say if oops and I didn't call that l2 so let me now let me say if l2 equals or l1 equals l2 then I will just print true and then else I will print false so right now these lists have different values so we're not going to expect these to be equal so when if I run this then we can see that we get false because those have different values but now let me set these lists to have the same values and see what they get so now they both have one two three four five so now if I run this then we can see that it prints out true that the lists are equal but values can be considered equal without actually being the same object in memory just like in the example where I said our two camps of Pepsi could be considered equal because they contain the same contents but even if they're equal there's still two different objects but if we do want to check if they're the same object in memory then that's when we use the is keyword so it checks the memory

Segment 2 (05:00 - 08:00)

addresses of the objects and it tells us if those are the same so if I can change if I change my conditional here to B if l1 is l2 if I run that then we can see that is false but now let me set my second list equal to the first one so if I say L 2 is equal to L 1 now the second list is going to be equal to the exact same list object in memory as our first list so now if I run this then we can see that now that prints out true now since list objects are mutable and these are equal to the same list object and memory it means that if we change one then it will also change the other so let me do that and see if we can see how that looks so I'm going to comment out our if statement here and I'm just gonna say l1 I'm gonna set the first value of l1 instead of 1 I'm going to set that equal to 6 and now I'm going to print out both l1 and printout l2 so if I run that now we can see that when we change that value of l1 it also changed the first value of l2 as well because those are now equal to the same object in memory now the reason that does this is because lists are mutable if you'd like to learn more about mutable and immutable objects then I'll leave a link to my video specifically on that subject in the description section below if anyone is interested so like I was saying earlier the is keyword compares the objects memory locations to see if it's pointing to the same object so we can actually see an object's memory address by passing it in to the ID function so I'm just going to take this line out here and I'm also going to remove this print statement I'll or this conditional I don't think I'm using that anymore so now let me print out the memory locations by passing these into the ID function and we are printing that out so if I run this then that prints out the objects memory address and in this case they're the same so really what the is keyword is doing in the background is checking if the memory addresses are equal so we could write that using a double equals as well so instead of using the is keyword we could say if and I probably shouldn't have deleted my conditional because I am using it again actually I won't I'll just print this out so I'm just going to print out I'm gonna say ID of l1 is equal to the ID of l2 so if I save that and run it then we can see that prints out true that the ID of l1 was equal to the ID of l2 so this here is basically what that is keyword is doing in the background okay so I hope that made sense okay so I think that is going to do it for this video I hope that cleared up the difference between the double equals comparison and using the is keyword but if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoyed these tutorials and would like to support them then there are several ways you can do that the easiest ways to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in a description section below be sure to subscribe for future videos and thank you all for watching you

Другие видео автора — Corey Schafer

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник