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

Corey Schafer · Beginner ·🛠️ AI Tools & Apps ·6y ago
Skills: Prompt Craft60%

Key Takeaways

The video explains the difference between using == and the is keyword in Python for comparisons, with a focus on equality vs identity, and demonstrates how to use the ID function to get the memory address of an object.

Full Transcript

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 had a can of 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 call that l2 so now let me say if l2 equals equals or l1 equals 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 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 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 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 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 equal to the ID of l2 so if I save that and run it then we can see that 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 that made sense okay so I think that is going to do it for this video I hope that 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 you

Original Description

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
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Corey Schafer · Corey Schafer · 0 of 60

← Previous Next →
1 Web fonts using CSS Font Face
Web fonts using CSS Font Face
Corey Schafer
2 Using Font Awesome in Desktop Applications (OS X)
Using Font Awesome in Desktop Applications (OS X)
Corey Schafer
3 Sublime Text 2: Setup, Package Control, and Settings
Sublime Text 2: Setup, Package Control, and Settings
Corey Schafer
4 ArcGIS API for JavaScript Part 1: Our First Web Map
ArcGIS API for JavaScript Part 1: Our First Web Map
Corey Schafer
5 Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Corey Schafer
6 Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Corey Schafer
7 ArcGIS API for JavaScript Part 2: Starting Templates
ArcGIS API for JavaScript Part 2: Starting Templates
Corey Schafer
8 Paver Patio Time Lapse
Paver Patio Time Lapse
Corey Schafer
9 Mac Tip: Ways to perform Screen Capturing and Screenshots
Mac Tip: Ways to perform Screen Capturing and Screenshots
Corey Schafer
10 WordPress Plugins: Imsanity
WordPress Plugins: Imsanity
Corey Schafer
11 WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
Corey Schafer
12 Sublime Text 3: Setup, Package Control, and Settings
Sublime Text 3: Setup, Package Control, and Settings
Corey Schafer
13 Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Corey Schafer
14 Mac Tip: Adding Folder Stacks to the Dock
Mac Tip: Adding Folder Stacks to the Dock
Corey Schafer
15 CSS Tips and Tricks: Add External URLs to Print Stylesheets
CSS Tips and Tricks: Add External URLs to Print Stylesheets
Corey Schafer
16 JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
Corey Schafer
17 JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
Corey Schafer
18 JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
Corey Schafer
19 JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
Corey Schafer
20 JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
Corey Schafer
21 JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
Corey Schafer
22 JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
Corey Schafer
23 Python Tutorial: if __name__ == '__main__'
Python Tutorial: if __name__ == '__main__'
Corey Schafer
24 Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Corey Schafer
25 How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
Corey Schafer
26 Easily Resize Multiple Images Using Picasa
Easily Resize Multiple Images Using Picasa
Corey Schafer
27 Easily Resize Multiple Images Using the Mac Terminal
Easily Resize Multiple Images Using the Mac Terminal
Corey Schafer
28 Python Tutorial: virtualenv and why you should use virtual environments
Python Tutorial: virtualenv and why you should use virtual environments
Corey Schafer
29 Python Tutorial: pip - An in-depth look at the package management system
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer
30 Git Tutorial: Using the Stash Command
Git Tutorial: Using the Stash Command
Corey Schafer
31 How Software Engineers, Developers, and Designers can volunteer their skills
How Software Engineers, Developers, and Designers can volunteer their skills
Corey Schafer
32 Git Tutorial: Diff and Merge Tools
Git Tutorial: Diff and Merge Tools
Corey Schafer
33 Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Corey Schafer
34 Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Corey Schafer
35 Python Tutorial: str() vs repr()
Python Tutorial: str() vs repr()
Corey Schafer
36 Programming Terms: DRY (Don't Repeat Yourself)
Programming Terms: DRY (Don't Repeat Yourself)
Corey Schafer
37 Programming Terms: String Interpolation
Programming Terms: String Interpolation
Corey Schafer
38 Programming Terms: Idempotence
Programming Terms: Idempotence
Corey Schafer
39 Python Tutorial: Namedtuple - When and why should you use namedtuples?
Python Tutorial: Namedtuple - When and why should you use namedtuples?
Corey Schafer
40 Programming Terms: Mutable vs Immutable
Programming Terms: Mutable vs Immutable
Corey Schafer
41 Python Tutorial: Else Clauses on Loops
Python Tutorial: Else Clauses on Loops
Corey Schafer
42 Overview of Online Learning Resources
Overview of Online Learning Resources
Corey Schafer
43 Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Corey Schafer
44 Git Tutorial for Beginners: Command-Line Fundamentals
Git Tutorial for Beginners: Command-Line Fundamentals
Corey Schafer
45 Quickest and Easiest Way to Run a Local Web-Server
Quickest and Easiest Way to Run a Local Web-Server
Corey Schafer
46 Python Tutorial: Generators - How to use them and the benefits you receive
Python Tutorial: Generators - How to use them and the benefits you receive
Corey Schafer
47 Python Tutorial: Comprehensions - How they work and why you should be using them
Python Tutorial: Comprehensions - How they work and why you should be using them
Corey Schafer
48 Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Corey Schafer
49 Programming Terms: Combinations and Permutations
Programming Terms: Combinations and Permutations
Corey Schafer
50 Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Corey Schafer
51 Preparing for a Python Interview: 10 Things You Should Know
Preparing for a Python Interview: 10 Things You Should Know
Corey Schafer
52 SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
Corey Schafer
53 SQL Tutorial for Beginners 2: Creating Your First Table
SQL Tutorial for Beginners 2: Creating Your First Table
Corey Schafer
54 SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
Corey Schafer
55 Linux/Mac Terminal Tutorial: Navigating your Filesystem
Linux/Mac Terminal Tutorial: Navigating your Filesystem
Corey Schafer
56 Python: Ex Machina Easter Egg - Hidden Message within the Code
Python: Ex Machina Easter Egg - Hidden Message within the Code
Corey Schafer
57 Mac Tip: New Split Screen Feature in El Capitan
Mac Tip: New Split Screen Feature in El Capitan
Corey Schafer
58 Setting up a Python Development Environment in Eclipse
Setting up a Python Development Environment in Eclipse
Corey Schafer
59 Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Corey Schafer
60 SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Corey Schafer

This video teaches the difference between == and is in Python, and how to use the ID function to get the memory address of an object. It demonstrates how equality and identity are not always the same thing, and how to compare values and objects in Python.

Key Takeaways
  1. Create two lists and compare them using == and is
  2. Use the ID function to get the memory address of an object
  3. Compare the memory addresses of two variables using the is keyword
  4. Change the contents of a list to demonstrate mutability
  5. Compare the memory addresses of two variables using the == operator
💡 The double equals operator (==) checks for equality of values, while the is keyword checks for identity (i.e., whether two variables point to the same object in memory).

Related AI Lessons

Up next
AI in Care - Katie Furey, Pairly.com
The Access Group
Watch →