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

  Рет қаралды 121,972

Corey Schafer

Corey Schafer

Күн бұрын

Пікірлер
@pepehimovic3135
@pepehimovic3135 5 жыл бұрын
⚽ = "football" 🏈 = "football" ⚽ == 🏈 >>> True ⚽ is 🏈 >>> False
@ghostriley22
@ghostriley22 5 жыл бұрын
football != handegg
@coreyms
@coreyms 5 жыл бұрын
Haha. That’s great
@vaguebrownfox
@vaguebrownfox 5 жыл бұрын
neat
@ViratKohli-jj3wj
@ViratKohli-jj3wj 5 жыл бұрын
Thats great way of explanation, I appreciate it
@angjohnsyin9517
@angjohnsyin9517 5 жыл бұрын
Thanks zlatan
@Remls
@Remls 5 жыл бұрын
'coke' == 'pepsi' *_BLASPHEMY!_*
@shreyashkashyap
@shreyashkashyap 3 жыл бұрын
Extremely well explained! It was a bit confusing in the pepsi example but the programming example made it all clear.
@Xartab
@Xartab 5 жыл бұрын
I had no idea lists would behave like that, that's really important to keep in mind when assigning variables!
@scarletdcruz7843
@scarletdcruz7843 5 жыл бұрын
Are you writing a book? Cause I'd buy it.
@rsrazorfen
@rsrazorfen 5 жыл бұрын
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!
@umaraftab6073
@umaraftab6073 5 жыл бұрын
I just watched your video on Unit Testing and wished to have you upload the next one. And, then here it is. :)
@Gaspar.Albertengo
@Gaspar.Albertengo 5 жыл бұрын
Excellent. Would be nice to see how "is" interacts with "not", "in", ect.
@nythepegasus
@nythepegasus 4 жыл бұрын
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 4 жыл бұрын
@@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.
@TexasGunDude
@TexasGunDude 5 жыл бұрын
You just got a new Patreon supporter.
@coreyms
@coreyms 5 жыл бұрын
Thanks!!
@PandhuW
@PandhuW 8 ай бұрын
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
@leondaz
@leondaz 5 жыл бұрын
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
@karimkohel3240
@karimkohel3240 5 жыл бұрын
this is the best type of content please keep educating us like that
@Ghasakable
@Ghasakable 5 жыл бұрын
Man we missed you so much!!, I
@scotttct
@scotttct 5 жыл бұрын
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!
@zakariyaabubakar6298
@zakariyaabubakar6298 5 жыл бұрын
Thanks Corey. Your a gifted teacher
@UrstrulyPrathap
@UrstrulyPrathap 5 жыл бұрын
Thank you so much sir..... finally I got Python developer job ❤️ ..only because of your tutorials❤️
@nicoTheGreatswordUser
@nicoTheGreatswordUser 5 жыл бұрын
How did u start in python
@coreyms
@coreyms 5 жыл бұрын
Congrats!
@poke_champ
@poke_champ 5 жыл бұрын
Corey Schafer uploads *[EVERYBODY LIKE THAT]*
@SeaTaj
@SeaTaj 3 жыл бұрын
Thank you. Great content as usual. Is it weird that I'm binging your content?
@ulfgj
@ulfgj 3 жыл бұрын
@3:46 - now how did u space out that list so nicely? shortcut?
@nehat786
@nehat786 5 жыл бұрын
thank god you came back
@aashayamballi
@aashayamballi 5 жыл бұрын
Thanks Corey 😊
@rainbowskeppy5292
@rainbowskeppy5292 3 жыл бұрын
3:55 you could just write print(l1 == l2)
@rodwanbakkar2512
@rodwanbakkar2512 4 жыл бұрын
I like your videos before watching them
@glorytoarstotzka330
@glorytoarstotzka330 5 жыл бұрын
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 5 жыл бұрын
Yes. I just did it with the conditional at first so that it was more clear what was going on.
@davidm.johnston8994
@davidm.johnston8994 5 жыл бұрын
Thank you for this video Corey!
@cati4142
@cati4142 5 жыл бұрын
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
@lethanhtu125
@lethanhtu125 5 жыл бұрын
Your video really easy to understand
@leondaz
@leondaz 5 жыл бұрын
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
@LuisSanchezLouieLouie
@LuisSanchezLouieLouie 4 жыл бұрын
Great example man. thanks
@changanzhang3124
@changanzhang3124 5 жыл бұрын
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 5 жыл бұрын
I think he should make a video on how he learns all the new stuff.
@khalidhassani6173
@khalidhassani6173 5 жыл бұрын
KZbin has got a talent is Schafer
@NomadLovesUs
@NomadLovesUs Жыл бұрын
Clear and concise, thank you!
@fredwooten14
@fredwooten14 5 жыл бұрын
Thanks Corey, Great video!
@rounakghosh6213
@rounakghosh6213 Жыл бұрын
Awesome, thanks for the clear explanation..
@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?
@ulfgj
@ulfgj 5 жыл бұрын
corey, whats the color theme for sublime you are using here? it's so smoooth.
@coreyms
@coreyms 5 жыл бұрын
Predawn
@rajath1964
@rajath1964 5 жыл бұрын
@@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
@EnglishRain
@EnglishRain Жыл бұрын
You are so fantastic i live you and miss you thank you so much for your amazing content!!
@jinyang4796
@jinyang4796 4 жыл бұрын
== 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.
@oltlatifi4870
@oltlatifi4870 5 жыл бұрын
Thank you for what you're doing
@souvikghosh6966
@souvikghosh6966 5 жыл бұрын
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!!!
@khireddinebelkhiri725
@khireddinebelkhiri725 5 жыл бұрын
do you know bro u are great thank you from algeria
@thinktech2936
@thinktech2936 Жыл бұрын
Very well explained
@blueexorcist4300
@blueexorcist4300 5 жыл бұрын
what happens if you use the is on two primitives?
@ericdrumond_ancap
@ericdrumond_ancap 5 жыл бұрын
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.
@coreycarter5668
@coreycarter5668 5 жыл бұрын
Even though I already understood this concept.....just going to watch the video because well-he’s amazing!
@adityaprasaddhal2462
@adityaprasaddhal2462 5 жыл бұрын
Thanks for knowledge
@aikimark1955
@aikimark1955 5 жыл бұрын
I wish you had included a "Type() is" example as well as the .copy() method for your second list
@krishnachaitanya1078
@krishnachaitanya1078 5 жыл бұрын
Thank you corey
@balzerno1
@balzerno1 5 жыл бұрын
Its funny how i was looking at the exact same thing just before clicking on this video haha
@legend7890
@legend7890 4 жыл бұрын
really good explanation
@linuxrant
@linuxrant Жыл бұрын
wonderful video!
@robot341
@robot341 5 жыл бұрын
This is Important topic, Good !!
@Yashparwal1
@Yashparwal1 2 жыл бұрын
thanks for the explanation
@MJFAN666
@MJFAN666 5 жыл бұрын
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?
@yoyolubi
@yoyolubi 6 ай бұрын
Let me se if i got it right... so basicaly when you use l2 = l1 in Python is the same thing as you use int* l2 = l1 to point to the l1 array in C, both lists (or arrays) end up refering to the same memory address? But when i use l1, l2 = 100, 100 print(l1 is l2) It returns true, so why? I mean... i didnt use l1 = l2 although they have the same value... Is it because both are the same instance of the integer class in Python, and by that refers to the same object when l1 and l2 variables recives their values?
@franciscorossi5633
@franciscorossi5633 4 жыл бұрын
Could anybody tell me which program or text editor is he using to run Python in this video? Thanks
@Miyota_9015
@Miyota_9015 5 жыл бұрын
Thanks bro, any chance of you recording a series on Flutter in the future? that would be amazing!
@mohsam3868
@mohsam3868 5 жыл бұрын
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 5 жыл бұрын
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 5 жыл бұрын
I think when performing this division python uses very close approximation of the numbers instead of the actual values
@Bread-vk8fl
@Bread-vk8fl 3 жыл бұрын
How to comment everything like he did at 5:59?
@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
@arar4454
@arar4454 5 жыл бұрын
Can you make some videos to : How i make exponential fits and gaussian fits
@mauricioyaman570
@mauricioyaman570 5 жыл бұрын
excelent explanation
@scifregizmoguy
@scifregizmoguy 5 жыл бұрын
# An `is` expression: A is B # Will evaluate to a boolean. # An equivalent expression: id(A) == id(B)
@AjayJain-ef2mz
@AjayJain-ef2mz 4 жыл бұрын
what about id(A) is id(B)?
@hamidsk2573
@hamidsk2573 5 жыл бұрын
dear Corey can you made python tutorial for the advance programming feature in python like multitasking , multithreading and async task
@AbdurRahman-iy7nd
@AbdurRahman-iy7nd 4 жыл бұрын
Thanks for your definition. I got the point. But the example with pepsi is not so relevant.
@tinman222
@tinman222 5 жыл бұрын
Hey Corey , i am using ckeditor in a django blog app hosted on digital ocean , and using digital ocean spaces for serving static files which uses amazon s3, but when it is not working properly can u plz make a video on it, i am debugging for atleast 10 days ckeditor uploader not working and even when it says paste the url it also not working, plz plz plz help me plz its very important and urgent bcoz my app is already on server
@benx5781
@benx5781 5 жыл бұрын
🏀=ball 🎾=ball 🏀=🎾 if 🏀==🎾: print("🏀==🎾") if 🏀 is 🎾: print("🏀 is 🎾") Here output will be: 🏀==🎾 🏀 is 🎾
@ihalil95
@ihalil95 5 жыл бұрын
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 5 жыл бұрын
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 5 жыл бұрын
Check my answer to Avinash Pandey ( &lc=UgwEAvkuTKLuEe27Kfh4AaABAg.8yIgY8IB_8f8yIrgMw5R6u )
@gfddgitgud3848
@gfddgitgud3848 2 жыл бұрын
Thank you Thank you Thank you !!!
@delapuenteignacio
@delapuenteignacio 5 жыл бұрын
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 5 жыл бұрын
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
@aggieaggie3946
@aggieaggie3946 5 жыл бұрын
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
@tinman222
@tinman222 5 жыл бұрын
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
@pushpajitbiswas3752
@pushpajitbiswas3752 5 жыл бұрын
Good explanation, can you make a video over pandas, numpy, opencv any one of these?
@TeguhGunawan
@TeguhGunawan 5 жыл бұрын
Thanks 👍
@geoClexE
@geoClexE 3 жыл бұрын
thank you so much !!!
@vkrs3315
@vkrs3315 2 жыл бұрын
Thank you so much
@ember9361
@ember9361 Жыл бұрын
it did! thank you :)
@tyt00341
@tyt00341 5 жыл бұрын
Thanks u teach well may I check which python IDLE are u using thanks
@lelgenio
@lelgenio 5 жыл бұрын
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
@geeseguy2715
@geeseguy2715 2 жыл бұрын
List = [1 , 2] "Okay" List[1] = 1 "Okay" List[2] = 1 "Okay" List [1] == List[2] --> True "Makes sense" List[1] is List[2] --> True ?????????????????
@G_a_n_d_u
@G_a_n_d_u 3 жыл бұрын
Corey Schafer ! = others programmer❤️
@0Navin0
@0Navin0 2 жыл бұрын
Thank you.
@GK-lf1ll
@GK-lf1ll 5 жыл бұрын
Why do we have two variable names for same memory location? Where can we apply in programming?
@s1n7ax
@s1n7ax 5 жыл бұрын
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.
@ginopornasdoro9183
@ginopornasdoro9183 5 жыл бұрын
do you have laravel tutorial?
@samytamim2603
@samytamim2603 Жыл бұрын
can we use is keyword with Primitive Values ?
@andrewluo6088
@andrewluo6088 5 жыл бұрын
you are the best teacher
@RichellyItalo
@RichellyItalo 5 жыл бұрын
Please, what's your font family?
@coreyms
@coreyms 5 жыл бұрын
Source Code Pro
@RichellyItalo
@RichellyItalo 5 жыл бұрын
@@coreyms thanks. One more thing. And the color scheme?
@coreyms
@coreyms 5 жыл бұрын
Richelly Italo Predawn. I have a video on how I set up my Sublime Text if you want to see the full setup.
@RichellyItalo
@RichellyItalo 5 жыл бұрын
@@coreyms Thanks man!!! I'm watching now.
@needsomehike
@needsomehike 5 жыл бұрын
what text editor are you using in these videos? (does anybody know?)
@andreamagista6742
@andreamagista6742 5 жыл бұрын
Sublime text
@needsomehike
@needsomehike 5 жыл бұрын
@@andreamagista6742 thank you!!
@maievmaiev8327
@maievmaiev8327 5 жыл бұрын
how can i copy l1 in another list named l2, and when changing l2 it wont affect l1, cause by copiying with =, the changes done on l1 will accure on l2
@coreyms
@coreyms 5 жыл бұрын
Hey there. Great question. Instead of giving all the different options in a long comment, I think this StackOverflow answer does a great job of explaining your different options: stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list
@dmitryvlasov931
@dmitryvlasov931 5 жыл бұрын
l2=l1.copy()
@maievmaiev8327
@maievmaiev8327 5 жыл бұрын
@@coreyms thank u
@maievmaiev8327
@maievmaiev8327 5 жыл бұрын
thank u!
@muhtadilaskar7650
@muhtadilaskar7650 5 жыл бұрын
You are awesome
@fantasticghosts7366
@fantasticghosts7366 5 жыл бұрын
Nice.
@rubemjr9623
@rubemjr9623 5 жыл бұрын
So is the is keyword equivalent to comparing C pointers?
@umaraftab6073
@umaraftab6073 5 жыл бұрын
Hey Corey, can you please make a short video explaining '_' and '__' used in variable declaration in Python? Thanks
@FixDaily
@FixDaily 3 жыл бұрын
That has to do with Private ( _ _ ) or Protect ( _ ) properties on Classes.
@ameerikbal8160
@ameerikbal8160 5 жыл бұрын
s = "india" b = "india" print(s is b) this code gives me a True value
@coreyms
@coreyms 5 жыл бұрын
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.
@ricardoseung1785
@ricardoseung1785 4 жыл бұрын
Is the command "is" the same as command "="? thank you!
@tiagu8085
@tiagu8085 4 жыл бұрын
No. '=' is assignment
@ldizai
@ldizai 5 жыл бұрын
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.
@desmondade
@desmondade 5 жыл бұрын
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 5 жыл бұрын
Straight Emirati thanks!! I’ll test it out
@desmondade
@desmondade 5 жыл бұрын
@@ldizai yeah just try it out any problem I will be glad to help
@j0584924
@j0584924 3 жыл бұрын
It should be noted that "is" works a bit differently for mutable and immutable types...
@elgalvin
@elgalvin 4 жыл бұрын
Advise you to use Ctrl+D to duplicate the code on the next string
@sainco3036
@sainco3036 5 жыл бұрын
thanks.
@RameenFallschirmjager
@RameenFallschirmjager 5 жыл бұрын
great explanation. 'banana' is 'cucumber', because both of them are slender and they are fruit! have I got the gist of this video right?!
@tulsivanol
@tulsivanol 5 жыл бұрын
Hello sir, Can u make a tutorial on numpy and pandas?
@leemoore8154
@leemoore8154 4 жыл бұрын
This doesn't seem to work with integer values. a = 5 b = 10 - 5 print(a is b) ----- True If you change it to b = 10/2 to get a float, then it prints False, but if they're both integers with the same value, they seem to be stored in the same place in memory.
Python Tutorial: if __name__ == '__main__'
8:43
Corey Schafer
Рет қаралды 2 МЛН
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 157 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Learn JSON in 10 Minutes
12:00
Web Dev Simplified
Рет қаралды 3,3 МЛН
Avoid These BAD Practices in Python OOP
24:42
ArjanCodes
Рет қаралды 82 М.
Как устроен PYTHON
37:44
про АйТи | IT Pro
Рет қаралды 29 М.
Memory Management in Python - The Basics
30:27
North Bay Python
Рет қаралды 37 М.
Python dataclasses will save you HOURS, also featuring attrs
8:50