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

  Рет қаралды 117,885

Corey Schafer

Corey Schafer

4 жыл бұрын

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 - • Programming Terms: Mut...
✅ Support My Channel Through Patreon:
/ coreyms
✅ Become a Channel Member:
/ @coreyms
✅ One-Time Contribution Through PayPal:
goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - coreyms.com/
My Second Channel - / coreymschafer
Facebook - / coreymschafer
Twitter - / coreymschafer
Instagram - / coreymschafer
#Python

Пікірлер: 213
@pepehimovic3135
@pepehimovic3135 4 жыл бұрын
⚽ = "football" 🏈 = "football" ⚽ == 🏈 >>> True ⚽ is 🏈 >>> False
@ghostriley22
@ghostriley22 4 жыл бұрын
football != handegg
@coreyms
@coreyms 4 жыл бұрын
Haha. That’s great
@vaguebrownfox
@vaguebrownfox 4 жыл бұрын
neat
@ViratKohli-jj3wj
@ViratKohli-jj3wj 4 жыл бұрын
Thats great way of explanation, I appreciate it
@angjohnsyin9517
@angjohnsyin9517 4 жыл бұрын
Thanks zlatan
@scarletdcruz7843
@scarletdcruz7843 4 жыл бұрын
Are you writing a book? Cause I'd buy it.
@Remls
@Remls 4 жыл бұрын
'coke' == 'pepsi' *_BLASPHEMY!_*
@shreyashkashyap
@shreyashkashyap 2 жыл бұрын
Extremely well explained! It was a bit confusing in the pepsi example but the programming example made it all clear.
@umaraftab6073
@umaraftab6073 4 жыл бұрын
I just watched your video on Unit Testing and wished to have you upload the next one. And, then here it is. :)
@Xartab
@Xartab 4 жыл бұрын
I had no idea lists would behave like that, that's really important to keep in mind when assigning variables!
@karimkohel3240
@karimkohel3240 4 жыл бұрын
this is the best type of content please keep educating us like that
@Gaspar.Albertengo
@Gaspar.Albertengo 4 жыл бұрын
Excellent. Would be nice to see how "is" interacts with "not", "in", ect.
@nythepegasus
@nythepegasus 3 жыл бұрын
I know this is a very old comment, but you cannot use is with in, it throws a SyntaxError. “is not” gives a SyntaxWarning asking if you meant “!=“
@lukebircharchive3833
@lukebircharchive3833 3 жыл бұрын
@@nythepegasus The syntax warning is because you probably should've used "!=" or not "==" in that case instead of "is not". If you type in "2 is 3", it would probably show you the same warning.
@zakariyaabubakar6298
@zakariyaabubakar6298 4 жыл бұрын
Thanks Corey. Your a gifted teacher
@davidm.johnston8994
@davidm.johnston8994 4 жыл бұрын
Thank you for this video Corey!
@rsrazorfen
@rsrazorfen 4 жыл бұрын
Hi Corey, I've been watching many of your videos (especially the flask-tutorial) and just wanted to thank you. Those videos basically got me started with python and helped me not only for my personal coding but also for my job. You're doing an amazing work - keep it up man, much love from Germany!
@scotttct
@scotttct 4 жыл бұрын
I am taking a course on Python and this was very helpful to see how and why the == and is comparisons are used in code. Thanks!
@fredwooten14
@fredwooten14 4 жыл бұрын
Thanks Corey, Great video!
@NomadLovesUs
@NomadLovesUs Жыл бұрын
Clear and concise, thank you!
@leondaz7380
@leondaz7380 4 жыл бұрын
I love you man, really thanks, I came to the newest video you posted just to thank you for everything, I hope you see this
@greob
@greob 4 жыл бұрын
Nice and clear explanation. Thanks.
@I_Follow_Jesus
@I_Follow_Jesus 4 жыл бұрын
You just got a new Patreon supporter.
@coreyms
@coreyms 4 жыл бұрын
Thanks!!
@oltlatifi4870
@oltlatifi4870 4 жыл бұрын
Thank you for what you're doing
@Ghasakable
@Ghasakable 4 жыл бұрын
Man we missed you so much!!, I
@aashayamballi
@aashayamballi 4 жыл бұрын
Thanks Corey 😊
@rounakghosh6213
@rounakghosh6213 Жыл бұрын
Awesome, thanks for the clear explanation..
@LuisSanchezLouieLouie
@LuisSanchezLouieLouie 3 жыл бұрын
Great example man. thanks
@thinktech2936
@thinktech2936 6 ай бұрын
Very well explained
@Yashparwal1
@Yashparwal1 Жыл бұрын
thanks for the explanation
@krishnachaitanya1078
@krishnachaitanya1078 4 жыл бұрын
Thank you corey
@adityaprasaddhal2462
@adityaprasaddhal2462 4 жыл бұрын
Thanks for knowledge
@lathamannam1591
@lathamannam1591 9 ай бұрын
Very good explanation
@linuxrant
@linuxrant Жыл бұрын
wonderful video!
@nehat786
@nehat786 4 жыл бұрын
thank god you came back
@legend7890
@legend7890 3 жыл бұрын
really good explanation
@cati4142
@cati4142 4 жыл бұрын
I really love your work! Thank you so much for your videos! Will you publish something about statistics with python? hypothese testing, linear regression or something? Would be sooo great
@geoClexE
@geoClexE 2 жыл бұрын
thank you so much !!!
@vkrs3315
@vkrs3315 Жыл бұрын
Thank you so much
@gfddgitgud3848
@gfddgitgud3848 2 жыл бұрын
Thank you Thank you Thank you !!!
@mauricioyaman570
@mauricioyaman570 4 жыл бұрын
excelent explanation
@robot341
@robot341 4 жыл бұрын
This is Important topic, Good !!
@0Navin0
@0Navin0 2 жыл бұрын
Thank you.
@ember9361
@ember9361 Жыл бұрын
it did! thank you :)
@TeguhGunawan
@TeguhGunawan 4 жыл бұрын
Thanks 👍
@rodwanbakkar2512
@rodwanbakkar2512 4 жыл бұрын
I like your videos before watching them
@PandhuW
@PandhuW 2 ай бұрын
I like your explanation. I also like your pandas tutorial video. Btw, I saw this in a book a = [1, 2, 3] c = list(a) a == c >>> True a is c >>> False
@EnglishRain
@EnglishRain Жыл бұрын
You are so fantastic i live you and miss you thank you so much for your amazing content!!
@khalidhassani6173
@khalidhassani6173 4 жыл бұрын
KZfaq has got a talent is Schafer
@changanzhang3124
@changanzhang3124 4 жыл бұрын
Corey you're awesome! How do you actually learn all these....and explain them in such a simple manner...and make these videos open source. Thanks!
@nathantum8463
@nathantum8463 4 жыл бұрын
I think he should make a video on how he learns all the new stuff.
@SeaTaj
@SeaTaj 2 жыл бұрын
Thank you. Great content as usual. Is it weird that I'm binging your content?
@davidcarter8932
@davidcarter8932 4 жыл бұрын
Your videos are exceptional. I enjoy them all. In this one it would help to expand a little on when to use "is" or ==. Linter seems to want me to use "is" in logical comparisons, but doesn't complain about == in other comparisons. So, should we use "value is True" and "x == y"? "Value is True" seems to work, but I don't trust it based upon your description about having identical memory locations. Are True and False assigned fixed memory locations and logical variables assigned one of those two memory locations? Since I don't know the answer, I am inclined to always use == which may not be pretty, but it works. What do you think?
@lethanhtu125
@lethanhtu125 4 жыл бұрын
Your video really easy to understand
@leondaz7380
@leondaz7380 4 жыл бұрын
I understand them perfectly because I made a project using them, but I still like if corey explains them, cause 100% I'll learn something new
@aikimark1955
@aikimark1955 4 жыл бұрын
I wish you had included a "Type() is" example as well as the .copy() method for your second list
@khireddinebelkhiri725
@khireddinebelkhiri725 4 жыл бұрын
do you know bro u are great thank you from algeria
@void_invoker
@void_invoker 4 жыл бұрын
Thanks bro, any chance of you recording a series on Flutter in the future? that would be amazing!
@ericdrumond_ancap
@ericdrumond_ancap 4 жыл бұрын
Hey man, I am from Brazil and I like a lot your videos. Do you have plans to make a Kivy Tutorials??? Thank you for the amazing videos.
@rainbowskeppy5292
@rainbowskeppy5292 3 жыл бұрын
3:55 you could just write print(l1 == l2)
@blueexorcist4300
@blueexorcist4300 4 жыл бұрын
what happens if you use the is on two primitives?
@UrstrulyPrathap
@UrstrulyPrathap 4 жыл бұрын
Thank you so much sir..... finally I got Python developer job ❤️ ..only because of your tutorials❤️
@norbertoignaciojr3365
@norbertoignaciojr3365 4 жыл бұрын
How did u start in python
@coreyms
@coreyms 4 жыл бұрын
Congrats!
@fantasticghosts7366
@fantasticghosts7366 4 жыл бұрын
Nice.
@arar4454
@arar4454 4 жыл бұрын
Can you make some videos to : How i make exponential fits and gaussian fits
@haroldaaron2098
@haroldaaron2098 4 жыл бұрын
Great videos about python, please make a video about comparison of Pass by value,reference and Pass by object reference in python, Hope to see more videos also in advance python
@sainco3036
@sainco3036 4 жыл бұрын
thanks.
@poke_champ
@poke_champ 4 жыл бұрын
Corey Schafer uploads *[EVERYBODY LIKE THAT]*
@hamidsk2573
@hamidsk2573 4 жыл бұрын
dear Corey can you made python tutorial for the advance programming feature in python like multitasking , multithreading and async task
@aggieaggie3946
@aggieaggie3946 4 жыл бұрын
hey Corey, thanks so much for the tutorials, they are really good but would you do a tutorial on how to all some one comment on your posts in your blog?thank you, i will really appreciate
@delapuenteignacio
@delapuenteignacio 4 жыл бұрын
One thing that might be worth adding is that if I define two integers rather than lists, then they both get stored in the same memory location. i.e. x = 3 y = 3 "x is y" outputsTrue. This won't happen with strings unless its just one word. So if x = 'a string' y = 'a string' "x is y" outputs False but if x = 'justoneword' y = 'justoneword' "x is y" outputs True. Go figure...
@rajath1964
@rajath1964 4 жыл бұрын
same holds good for string also, name = "tony" name1 = "tony" print(id(name)) print(id(name1)) if name is name1: print(True) else: print(False) ********************************* the above code returns same memory reference and True. i think he was specifically describing for lists
@pushpajitbiswas3752
@pushpajitbiswas3752 4 жыл бұрын
Good explanation, can you make a video over pandas, numpy, opencv any one of these?
@jinyang4796
@jinyang4796 3 жыл бұрын
== checks for equality is checks for identity (i.e. are the objects from the same memory address?) Values can be considered equal without being the same object in memory.
@mohsam3868
@mohsam3868 4 жыл бұрын
d=[0.25,0.10,0.50] def coin(num): for i in d: if num%i == 0: print(i) coin(1.25) coin(2.2) '''why the reminder in this case is not equal zero '''
@daniloreboucas5888
@daniloreboucas5888 4 жыл бұрын
2.2 % 0.1 returns a number very close to 0 instead idk why but on my console it is returning 5.5e-17 instead
@daniloreboucas5888
@daniloreboucas5888 4 жыл бұрын
I think when performing this division python uses very close approximation of the numbers instead of the actual values
@souvikghosh6966
@souvikghosh6966 4 жыл бұрын
If the names to the two persons are the same, then we call they are '==' to each other. If they look like exactly the same like twins then we call they are identical, which means they are 'is' to other. Nice!!!
@coreycarter5668
@coreycarter5668 4 жыл бұрын
Even though I already understood this concept.....just going to watch the video because well-he’s amazing!
@AbdurRahman-iy7nd
@AbdurRahman-iy7nd 3 жыл бұрын
Thanks for your definition. I got the point. But the example with pepsi is not so relevant.
@glorytoarstotzka330
@glorytoarstotzka330 4 жыл бұрын
4:15 question : could you also do "print(l1 == l2)" instead of "if l1 == l2: print(True) else: print(False)" ? . Edit: I see that at the end of the video you've done this , so my question has been answered
@coreyms
@coreyms 4 жыл бұрын
Yes. I just did it with the conditional at first so that it was more clear what was going on.
@balzerno1
@balzerno1 4 жыл бұрын
Its funny how i was looking at the exact same thing just before clicking on this video haha
@umaraftab6073
@umaraftab6073 4 жыл бұрын
Hey Corey, can you please make a short video explaining '_' and '__' used in variable declaration in Python? Thanks
@FixDaily
@FixDaily 2 жыл бұрын
That has to do with Private ( _ _ ) or Protect ( _ ) properties on Classes.
@muhtadilaskar7650
@muhtadilaskar7650 4 жыл бұрын
You are awesome
@thePronto
@thePronto 4 жыл бұрын
It depends on what the meaning of the word 'is' is.
@user-zi4if7ff6i
@user-zi4if7ff6i Жыл бұрын
Good
@tulsivanol
@tulsivanol 4 жыл бұрын
Hello sir, Can u make a tutorial on numpy and pandas?
@rubemjr9623
@rubemjr9623 4 жыл бұрын
So is the is keyword equivalent to comparing C pointers?
@ulfgj
@ulfgj 3 жыл бұрын
@3:46 - now how did u space out that list so nicely? shortcut?
@tinman222
@tinman222 4 жыл бұрын
Or can u make a video on how to implent a good wsywig editor which has file uploading and accesing functionality in in django app on front and also on admin panel , in text area
@tyt00341
@tyt00341 4 жыл бұрын
Thanks u teach well may I check which python IDLE are u using thanks
@lelgenio
@lelgenio 4 жыл бұрын
I think he uses Sublime text Also, IDLE is the name of the simple python IDE I would recommend you trying Thonny if you are just starting
@scifregizmoguy
@scifregizmoguy 4 жыл бұрын
# An `is` expression: A is B # Will evaluate to a boolean. # An equivalent expression: id(A) == id(B)
@AjayJain-ef2mz
@AjayJain-ef2mz 3 жыл бұрын
what about id(A) is id(B)?
@aymaneboukrouh
@aymaneboukrouh 4 жыл бұрын
Can you make a tutorial on PyCharm ?
@kamransiddique5731
@kamransiddique5731 4 жыл бұрын
Can you upload some videos on machine learning
@ulfgj
@ulfgj 4 жыл бұрын
corey, whats the color theme for sublime you are using here? it's so smoooth.
@coreyms
@coreyms 4 жыл бұрын
Predawn
@rajath1964
@rajath1964 4 жыл бұрын
@@coreyms how to comment multiple lines at once using '#' (single line comment), i see many youtubers do that but just cant figure it out. like what i mean is : use # to comment multiple selected /highlighted lines with single click
@elgalvin
@elgalvin 3 жыл бұрын
Advise you to use Ctrl+D to duplicate the code on the next string
@GK-lf1ll
@GK-lf1ll 4 жыл бұрын
Why do we have two variable names for same memory location? Where can we apply in programming?
@s1n7ax
@s1n7ax 4 жыл бұрын
If you have a object list, and you want to remove a given object from the list, you can use 'is' i guess. Not a python programmer but most of the languages are using 'is' concept by default so this is one use case.
@G_a_n_d_u
@G_a_n_d_u 2 жыл бұрын
Corey Schafer ! = others programmer❤️
@ginopornasdoro9183
@ginopornasdoro9183 4 жыл бұрын
do you have laravel tutorial?
@samytamim2603
@samytamim2603 8 ай бұрын
can we use is keyword with Primitive Values ?
@MJFAN666
@MJFAN666 4 жыл бұрын
That's weird.. So what if I say l2=l1 and modify l2? Will it also change l1?? What if I wanted to make a copy of the var without messing with mem location?
@Brootahlizer
@Brootahlizer 4 жыл бұрын
Just run that code already!
@franciscorossi5633
@franciscorossi5633 3 жыл бұрын
Could anybody tell me which program or text editor is he using to run Python in this video? Thanks
@Igor-vk8fl
@Igor-vk8fl 2 жыл бұрын
How to comment everything like he did at 5:59?
@rajm5349
@rajm5349 2 жыл бұрын
WHAT are suites
@andrewluo6088
@andrewluo6088 4 жыл бұрын
you are the best teacher
@syzygyfpv5135
@syzygyfpv5135 4 жыл бұрын
Title sounds very politic :)
@ameerikbal8160
@ameerikbal8160 4 жыл бұрын
s = "india" b = "india" print(s is b) this code gives me a True value
@coreyms
@coreyms 4 жыл бұрын
Sometimes smaller strings can share objects in memory. But you won't get the same behavior where changing one variable affects the other like a list because strings are immutable. With longer strings you shouldn't see that behavior.
@j0584924
@j0584924 3 жыл бұрын
It should be noted that "is" works a bit differently for mutable and immutable types...
@benx5781
@benx5781 4 жыл бұрын
🏀=ball 🎾=ball 🏀=🎾 if 🏀==🎾: print("🏀==🎾") if 🏀 is 🎾: print("🏀 is 🎾") Here output will be: 🏀==🎾 🏀 is 🎾
@ldizai
@ldizai 4 жыл бұрын
Hi Corey, thanks for all teh vids! Could you do one on how to check if the person is a subscription member or not in Python-Flask. I've learned everything in your Flask Blog app. So after that is done, how to set up members account for premium features via payment.
@desmondadefon6116
@desmondadefon6116 4 жыл бұрын
You can basically have a field in your user model name is_subscriber which is a bolean and set this value to true when a user subscribes in the payments route or you have a OneToOneField to add this extra info
@ldizai
@ldizai 4 жыл бұрын
Straight Emirati thanks!! I’ll test it out
@desmondadefon6116
@desmondadefon6116 4 жыл бұрын
@@ldizai yeah just try it out any problem I will be glad to help
@ihalil95
@ihalil95 4 жыл бұрын
Amazing video as always. My question is x = 2 y = 2 if x is y: print(True) x and y are different. but their id(x) and id(y) are the same. Why? Also when make y = 2.0 float its id changes.
@coreyms
@coreyms 4 жыл бұрын
That’s because some common values are immediately stored in memory when Python is first run for efficiency reasons. So sometimes you get behavior like this. But I wouldn’t rely on using “is” in that way in your programs because it won’t always be true.
@CT7ALW
@CT7ALW 4 жыл бұрын
Check my answer to Avinash Pandey ( &lc=UgwEAvkuTKLuEe27Kfh4AaABAg.8yIgY8IB_8f8yIrgMw5R6u )
Final muy increíble 😱
00:46
Juan De Dios Pantoja 2
Рет қаралды 45 МЛН
OMG🤪 #tiktok #shorts #potapova_blog
00:50
Potapova_blog
Рет қаралды 18 МЛН
OMG😳 #tiktok #shorts #potapova_blog
00:58
Potapova_blog
Рет қаралды 4,2 МЛН
Functions vs Classes: When to Use Which and Why?
10:49
ArjanCodes
Рет қаралды 144 М.
10 Python Tips and Tricks For Writing Better Code
39:21
Corey Schafer
Рет қаралды 1,3 МЛН
Python Tutorial: if __name__ == '__main__'
8:43
Corey Schafer
Рет қаралды 2 МЛН
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 271 М.
super/MRO, Python's most misunderstood feature.
21:07
mCoding
Рет қаралды 213 М.
Is THIS Python's MOST Underrated Operator? (Walrus Operator)
5:45
Python Tutorial: File Objects - Reading and Writing to Files
24:33
Corey Schafer
Рет қаралды 1,7 МЛН
Final muy increíble 😱
00:46
Juan De Dios Pantoja 2
Рет қаралды 45 МЛН