Amazing. Thanks to you, I have learned so much when I discovered your channel. My grandpa had told me a long time ago: "Knowing how to do something is not valuable, but knowing how to do something and why to do this in such a way is.". You give us pure knowledge. Thank you.
Hello Sir, THANK YOU SOO MUCH. i literally spend 3 hours to figure out how can i sort my complex tuple...and still didnt get it...Until i watch this video. Please keep up the great work. and thanks once again
Excellent video. Love it! The Employee class can be made sortable by defining the __lt__ method like def __lt__(self, other): return self.name < other.name
Hey, thanks for the video! I just started learning Python in order to get into Data Science and Machine Learning. These have been by far the simplest and easiest videos for me to understand and apply. Thank you!
@@aabhishek4911 Haven't really started. I'm currently learning software engineering for my university focus path. I'm learning Python and SQL on the side though.
Loving the series and wanted to say thank you for putting this out. I noticed due to a typo in my hand typing/copying the code and running it that it is worth mentioning that like other languages I have looked at (which would be just barely getting into C and basic) that sorting strings is based upon the ASCII set, which means that sorting the words 'OS' and 'age' due to lowercase being higher numbered the 'age' string is returned after the 'OS' string. These videos are awesome, TY again for doing this
Omg man these videos are beyond amazing. I know python but as i watch your tutorials I see how much extra bs I do that not needed!!!! I didn’t always thohhht that the function in key param should be a custom lambda...
Thank you. This helped so much. Now I can finish my clay shooter project and have a sorted list of names and high scores sorted by scores. Thank you very much!
First of all, your teaching style is good, and anyone grabs ur teachings easily, and thanks for teaching us. you are the best tutor I have ever come up with in python language. can u suggest any python language book, which is best to go through other than your channel? Moreover, I am a beginner to python language I thoroughly enjoyed ur videos, if u can suggest me best python book to ur sensibility and I guess it suits me and it helps me to learn further (I like physical like books). In case if u written any books related to this language can u suggest one (or) your favorite book, please......................................
@@easydatascience2508 Thank you. I will. My new job requires a lot of Python and I’ve grown to like it very much. It has a very rich library base. Keep coding :)
Wow! A hellava lot more effort to do the same thing in Java for a Java newb. Employee class. Excellent example. This video identified an area that I need to focus on. The __ whatever__ functions
Thats awesome one..whenever I see some coding of you I also try to do my way.. class Employee(): def __init__(self,n,a,s): self.n=n self.a=a self.s=s def __repr__(self): return '({},{},${})'.format(self.n,self.a,self.s) e1=Employee('carl',45,7000) e2=Employee('sarah',35,2000) e3=Employee('john',25,1000) ee=[e1,e2,e3] l=[] for a in ee: l.append(a.s) print(sorted((l))) Thanks for your awsome tutorial.. :)
Very useful, thanks. In another video about comprehensions, it was suggested to use a comprehension in place of lambdas for readability. How could that be achieved with the example in this video?
Great, didnt understand why we give (emp) in e_sort function first then you put lambda i understand it, emp was unnecessary as a function param so lambda directly shortens first e_sort func in a shortest form
please note that these videos are not numbered, so there is no particular order on how watch them.. what matters here is to just get the basic idea. Just get the basics, and if needed you can come back at it anytime
Hi. In which video did you talk about the class? that line of code related to class Employee() seems has not been cover before this course 21. Thank you
The argument name "emp" does not reference the actual class. When the function is given as the "key" argument in the "sorted" function, "sorted" will run "e_sort" on each object in the iterable (the list) and pair the returned value to the objects, so that "sorted" will be able to use the values that actually know how to be sorted. In fact, the "e_sort" function as written in the video, will work on any object that has a "name" attribute, since the function only attempts to get the "name" attribute from the object it's given and doesn't care what actual type the object is.
Hey Corey I saw the string formatting video but I don't know why you use '$' on the salary placeholder in __repr__ function. Is it about some int to str stuff? because i'm kinda lost on this, sorry if it is a dumb question ! Your python tutorials are wonderful btw, hope you make a django / flask playlist one day too.
Ringo gg Hey, that's a good question. It doesn't actually do anything in terms of formatting. If you look at the output when it's printed, it just added a dollar sign before the salary to signify it was a cash amount. But thanks for pointing that out because really I shouldn't have printed it out with a dollar sign within repr() since repr should usually be something you can use to recreate the object. That dollar sign may be a better output choice for str(). But to answer your question, it doesn't do anything in terms of formatting.
Thank you for the video. Can the "sorted" built-in method deal with ties and break them according to another attribute. For example, in your Employee class, is it possible to write a function (as argument for the key parameter) that sorts according to age and, if there is a tie, then sorts according to salary?
The key function described in this video takes only one argument. Say we need to sort based on employee name, then salary, then age. How will that work?
Hey Corey, excellent videos. However, there is no linear progression playlist of python. For example, to understand this video, I had to check out classes in python. Can you make a linear playlist of python. Thanks!
If I had a large data set with many attributes, is there a way that I could turn them all into objects in bulk, so that I could then proceed to sorting the data in a certain way?
At 8:30 why cannot we simply go as key = emp.name ? I did not understand the necessity of defining a new parameter. Corey please explain if you see this comment. Anyone else's advice is also appreciated. Thank you.
Stupid question but @9:46, how does the e_sort function receive it's parameter? I don't see you passing any values to it so I have to assume, it's getting those from the first argument of sorted() which was 'employees'?
They can be sorted, but you just have to have a way to tell Python what you want the Employee to be sorted on. Because when you have an object with multiple attributes, it may not be obvious which attributes you would like to sort on.
Thanks for quick replies. It really helps. I tend to forget my own question in a few days. While it is not very clear, I'll keep moving on and guess it'll be clear and too easy as I gain more knowledge.
Hi sir I use Pycharm for coding, but some keyword are not defined, and I try to use sublime text also but it still don't work. e.g print nums # which don't have parentheses.
Thank you for the great videos! Could you explain why emp is used in the def e_sort? I don't see how it is calling/referring to anything in the class Employee. Thank you.
Hi, emp is just the name he chose for the argument that is passed in the function.He could have also write def e_sort(abcdef): return abcdef.name and it would have worked the same.The point is that whatever you pass as parameter in the function,the function will return the value of the name attribute of that parameter(name being in this case the sorting criteria).So that function would have worked for any class that has a name attribute.So this function is just the sorting criteria of the sorted method.What connects this function to the employee class is these 2 lines of code:employees = [e1,e2,e3] and s_employees = sorted(employees,key=e_sort) (after of course creating e1,e2 and e3 as instances of the Employee class.) I know i'm late but I just started watching this series.Hope it helps
Hey there. It’s a bit much to explain in a comment, but I have a video on this. If you search my channel for “magic methods”, then it should come up. Thanks.
Im using python 3.6 and this does not work in my project, its like im trying to print the whole object: def e_sort(emp): return emp.name s_employee = sorted(employees, key=e_sort) print(s_employee) Can you tell me whats emp in def e_sort(emp) ?
Year late but whatever. When you call the function e_sort you pass in the parameter employees( the list-e1,e2,e3)Emp inside the function is a place holder variable for the entire list. Emp.name points to the name which was created in the class(which is now saved in employees list you passed through). Until you use the key=e_sort, the call to the built in function 'sorted' didn't know whether to sort by name, age or salary. Zero percent chance that made sense. Best of luck
Thanks for the video, can you tell me how to sort dictionaries based on values? I came across this use case couple of times before and had to google it to get the logic. But would like to know how you would do this?
Sorry to recommend the same answer you likely saw on StackOverflow, but that answer is the same way I would sort based on values: from operator import itemgetter x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} sorted_x = sorted(x.items(), key=itemgetter(1)) Link back to StackOverflow question: stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value
Hey There... I made a video on how I set up this Python development environment. It will show you the packages I use that do the code completions. You can find that video here: ruclips.net/video/xFciV6Ew5r4/видео.html Thanks!
@Corey Schafer First of all thank you for the videos and the amazing explanations!!! They have been a wonder. I would like to however find out what settings or packages you use to get the info on what it is that you are typing when coding. For example in this video at 0.56 while you are typing " s_li ".... it indicates that it is a statement. I have also seen that it does this for keywords and others as well. I have watched your video in configuring sublime text for python and have added the packages that work for me and have done the settings, however those settings have not added this functionality. Could you point me in the right direction so i can configure my sublime text to allow for this? Thank you
Hey friend! Thx for you work. Can you explain me how do you delete # symbol in SublimeText with a single click? I use mac OS and I Don't know the hotkeys to delete symbols in the few string at once
For those reading the comments: this channel is a goldmine!
Thanks!
No, you are wrong, this channel is one DIAMONDMINE which is more worth then GOLDMINE 😁😁😁😁
Indeed!!
You are very right! Corey makes learning so easy!
cant agree more!
I wonder how can someone dislike these tutorials
Amazing. Thanks to you, I have learned so much when I discovered your channel. My grandpa had told me a long time ago: "Knowing how to do something
is not valuable, but knowing how to do something and why to do this in such a way is.". You give us pure knowledge. Thank you.
Grandpa's wisdom >>>>>>>
The ease with which this person writes the code is ultra classy. I can only wish I had the knowledge as superior as Corey's.
Hello Sir, THANK YOU SOO MUCH. i literally spend 3 hours to figure out how can i sort my complex tuple...and still didnt get it...Until i watch this video. Please keep up the great work. and thanks once again
Excellent video. Love it! The Employee class can be made sortable by defining the __lt__ method like
def __lt__(self, other):
return self.name < other.name
Hey, thanks for the video! I just started learning Python in order to get into Data Science and Machine Learning. These have been by far the simplest and easiest videos for me to understand and apply. Thank you!
Hi John , How is going with my machine learning. I am learning python for machine learning. Hope to hear your advise. Thanks.
I'm also want to learn it, how did it go with you three people? xD
How did it go for the 4 of you ?
@@aabhishek4911 Haven't really started. I'm currently learning software engineering for my university focus path. I'm learning Python and SQL on the side though.
How’s it goin for the 5 of you?
Corey is teaching the things we need to know exactly. Thanks.
Your video is the best for Python learners !!!!!!!!
nice place to go back to the basics and a great road for beginners
Excellent way teaching
Loving the series and wanted to say thank you for putting this out. I noticed due to a typo in my hand typing/copying the code and running it that it is worth mentioning that like other languages I have looked at (which would be just barely getting into C and basic) that sorting strings is based upon the ASCII set, which means that sorting the words 'OS' and 'age' due to lowercase being higher numbered the 'age' string is returned after the 'OS' string. These videos are awesome, TY again for doing this
Omg man these videos are beyond amazing. I know python but as i watch your tutorials I see how much extra bs I do that not needed!!!! I didn’t always thohhht that the function in key param should be a custom lambda...
you're incredible at explaining stuff man
such a finest tuts i love it , thank you corey schafer
I like how every video that uses lambda always says 'im not gonna get into lambda'.
I feel like its not as hard as it seems? Maybe I am being ignorant
Don’t panic... take a look at anonymous functions in javascript, then come back to python, take a deep breath and relax :)
@@louistannudin2486 i agree, though sometimes i can't quite wrap my head around it and get confused
Thank you. This helped so much. Now I can finish my clay shooter project and have a sorted list of names and high scores sorted by scores. Thank you very much!
Good bless you
and God bless your courses
learning to get my goal to financially independent
and work in space company and start-ups!
First of all, your teaching style is good, and anyone grabs ur teachings easily, and thanks for teaching us. you are the best tutor I have ever come up with in python language. can u suggest any python language book, which is best to go through other than your channel? Moreover, I am a beginner to python language I thoroughly enjoyed ur videos, if u can suggest me best python book to ur sensibility and I guess it suits me and it helps me to learn further (I like physical like books). In case if u written any books related to this language can u suggest one (or) your favorite book, please......................................
the reason they removed the dislike button was because they did not want people to dislike this video because its so good
Awesome video, so very well explained. Thank you for sharing.
you can watch mine too. The channel provides most of the fundamentals for Python and R, with downoadable source files.
@@easydatascience2508 Thank you. I will. My new job requires a lot of Python and I’ve grown to like it very much. It has a very rich library base. Keep coding :)
Omg I have learned so much about this basic topic even tho I'm working whit classes!!
Thanks mr. Corey Schafer, very useful as usual!
Very clear, as always.
I was trying to do this exact thing just today. so glad I stumbled on this video. thanks!
Who would have thought sorting things was so easy! Thanks man, this is gonna be useful =D
Wow! A hellava lot more effort to do the same thing in Java for a Java newb. Employee class. Excellent example. This video identified an area that I need to focus on. The __ whatever__ functions
I have a video on that double underscore functions. If you search my channel for “special methods” you will find them there
Thats awesome one..whenever I see some coding of you I also try to do my way..
class Employee():
def __init__(self,n,a,s):
self.n=n
self.a=a
self.s=s
def __repr__(self):
return '({},{},${})'.format(self.n,self.a,self.s)
e1=Employee('carl',45,7000)
e2=Employee('sarah',35,2000)
e3=Employee('john',25,1000)
ee=[e1,e2,e3]
l=[]
for a in ee:
l.append(a.s)
print(sorted((l)))
Thanks for your awsome tutorial.. :)
i was here coz i had a problem sorting objects . Job well done. Thanks. If you open a school, i will be the first to enroll
Corey, Very nice and thanks for sharing.
thankyou, you help me for PTI-B homework
7:07
clearest explanation of #repr
Very useful, thanks. In another video about comprehensions, it was suggested to use a comprehension in place of lambdas for readability. How could that be achieved with the example in this video?
you are the best, you help me a lot TNX.
Thank you very much, mate! This video really helped me!
Video was head on. Thank you very much.
Thank you Corey for your time and effort. Keep spreading the knowledge.
you are amazing, thanks for these awesome videos
Key=Custom function -> lambda function->attrgettrer->itemgetter
Awesome video 🎉
Great, didnt understand why we give (emp) in e_sort function first then you put lambda i understand it, emp was unnecessary as a function param so lambda directly shortens first e_sort func in a shortest form
@4:45
3 lines of code to do what I've been struggling to do with 12-18 lines. Good work, sir.
Thank you!
Im learning according to the order in the playlist. Suddenly "class"appears when you didn't mention it at all previously.
Same, but there are videos on that subject. Take a pause and find that one, they are all gems!
bro before watching these videos watch his beginners tutorial . I did the same it really clarifies everything
please note that these videos are not numbered, so there is no particular order on how watch them.. what matters here is to just get the basic idea. Just get the basics, and if needed you can come back at it anytime
For 8:01 what if I wanted to put input values for the name age and salary how will I do that?
you saved my life
Hi. In which video did you talk about the class? that line of code related to class Employee() seems has not been cover before this course 21. Thank you
thank you :)
Hii corey
When u defined e_sort (emp) how the function knows how to return instance variable from the class ? ,
i have same question sir plzz answer
The argument name "emp" does not reference the actual class. When the function is given as the "key" argument in the "sorted" function, "sorted" will run "e_sort" on each object in the iterable (the list) and pair the returned value to the objects, so that "sorted" will be able to use the values that actually know how to be sorted.
In fact, the "e_sort" function as written in the video, will work on any object that has a "name" attribute, since the function only attempts to get the "name" attribute from the object it's given and doesn't care what actual type the object is.
@@thegamecracks1317 Thanks a lot for the clarification!!
Great video. Thank you!!
Such an amazing tutorial! Keep up the good work brother!
Your videos are awesome really loved them . please add videos on some popular libraries like pygame,openpyxl etc.
Thank you
thank you so much :)... very informative videos
Great vid. How about a video on linked-list vs. arrays, and sort algorithms?
Hey, at last you use the attrgetter method so what's the difference between the attrgetter and itemgetter
I am wondering if you should create a companion video for multiple key sort.
as usual great tutorial, hopefully I can finish the whole series up to date!
Hey Corey I saw the string formatting video but I don't know why you use '$' on the salary placeholder in __repr__ function. Is it about some int to str stuff? because i'm kinda lost on this, sorry if it is a dumb question ! Your python tutorials are wonderful btw, hope you make a django / flask playlist one day too.
Ringo gg Hey, that's a good question. It doesn't actually do anything in terms of formatting. If you look at the output when it's printed, it just added a dollar sign before the salary to signify it was a cash amount. But thanks for pointing that out because really I shouldn't have printed it out with a dollar sign within repr() since repr should usually be something you can use to recreate the object. That dollar sign may be a better output choice for str(). But to answer your question, it doesn't do anything in terms of formatting.
oh thanks for reply so quickly ! I didn't notice the dollar sign af first because they are too close with each other and it has no space in it lol
Absolutely amazing
Thank you for the video. Can the "sorted" built-in method deal with ties and break them according to another attribute. For example, in your Employee class, is it possible to write a function (as argument for the key parameter) that sorts according to age and, if there is a tie, then sorts according to salary?
The key function described in this video takes only one argument. Say we need to sort based on employee name, then salary, then age. How will that work?
you are just awesome man
so good tutorial videos! why you stop making new one?
God bless you Corey!!!
Hey Corey, excellent videos. However, there is no linear progression playlist of python. For example, to understand this video, I had to check out classes in python. Can you make a linear playlist of python. Thanks!
If you want name as primary sort and then age as secondary sort...how would you do that?
sorry i wanna ask, can i sorting value of list without change an index
thanks bro!
Amazing vid
Thanks you so much
Hey ur videos r awesome can u make videos on data structure and algorithm
on the function "e_sort()", how does salary, age or name is returned from the class?
How can we sort using earliest and latest datetime data
Pls do reply..thanks
good info. thanks.
If I had a large data set with many attributes, is there a way that I could turn them all into objects in bulk, so that I could then proceed to sorting the data in a certain way?
This is very helpful, thank you!
At 8:30 why cannot we simply go as key = emp.name ? I did not understand the necessity of defining a new parameter. Corey please explain if you see this comment.
Anyone else's advice is also appreciated. Thank you.
this guy overcomplicates simple task for no reason
Hey @corey, is this formatting still valid in the latest python?
Hi, thank you for all your videos. How do you comments out a line or several lines?
ctrl + /
what text editor you are using? Looks cool
Bill Deng likely sublime, or vs code, or atom
what if you were given (1,7),(1,3),(3,4,5),(2,2) n you were requested to sort using the last element of the tuple
How do you sort objects based on age first and then salary together, and suppose if ages are same and then, salary are same then sort with name .
This is a lot of stuff!
More power by the way.
Stupid question but @9:46, how does the e_sort function receive it's parameter? I don't see you passing any values to it so I have to assume, it's getting those from the first argument of sorted() which was 'employees'?
you should refer Corey's video regarding Variable scopes. he explains really thoroughly that topic
What if two employees have same name and we want to sort based on their salary then age. How would we go on for that?
How do you match or intersection two multidimensional list of a list
Why can't employees be sorted ? I mean... is it because it is a class created object ? or is it because it have multiple values within them ?
They can be sorted, but you just have to have a way to tell Python what you want the Employee to be sorted on. Because when you have an object with multiple attributes, it may not be obvious which attributes you would like to sort on.
Thanks for quick replies. It really helps. I tend to forget my own question in a few days. While it is not very clear, I'll keep moving on and guess it'll be clear and too easy as I gain more knowledge.
Hi sir I use Pycharm for coding, but some keyword are not defined, and I try to use sublime text also but it still don't work. e.g print nums # which don't have parentheses.
I have been following the python playlist and I am slightly confused by what was the use of the class here?
Great tutorial. However, I get
"TypeError: '
maybe you can try str() for the number. You can see my channel too, it has a Python playlist, most of the fundamentals. Hope they will be helpful.
Thank you for the great videos!
Could you explain why emp is used in the def e_sort? I don't see how it is calling/referring to anything in the class Employee. Thank you.
Hi, emp is just the name he chose for the argument that is passed in the function.He could have also write def e_sort(abcdef):
return abcdef.name
and it would have worked the same.The point is that whatever you pass as parameter in the function,the function will return the value of the name attribute of that parameter(name being in this case the sorting criteria).So that function would have worked for any class that has a name attribute.So this function is just the sorting criteria of the sorted method.What connects this function to the employee class is these 2 lines of code:employees = [e1,e2,e3] and s_employees = sorted(employees,key=e_sort) (after of course creating e1,e2 and e3 as instances of the Employee class.)
I know i'm late but I just started watching this series.Hope it helps
Hello,
Why we use init and repr as __init__ and __repr__... What is the logic behind putting the _ before and after them?
Hey there. It’s a bit much to explain in a comment, but I have a video on this. If you search my channel for “magic methods”, then it should come up. Thanks.
@@coreyms Thanks a lot!!! Will have a look in into it...
Im using python 3.6 and this does not work in my project, its like im trying to print the whole object:
def e_sort(emp):
return emp.name
s_employee = sorted(employees, key=e_sort)
print(s_employee)
Can you tell me whats emp in def e_sort(emp) ?
Year late but whatever. When you call the function e_sort you pass in the parameter employees( the list-e1,e2,e3)Emp inside the function is a place holder variable for the entire list. Emp.name points to the name which was created in the class(which is now saved in employees list you passed through). Until you use the key=e_sort, the call to the built in function 'sorted' didn't know whether to sort by name, age or salary.
Zero percent chance that made sense. Best of luck
Thanks for the video, can you tell me how to sort dictionaries based on values?
I came across this use case couple of times before and had to google it to get the logic. But would like to know how you would do this?
Sorry to recommend the same answer you likely saw on StackOverflow, but that answer is the same way I would sort based on values:
from operator import itemgetter
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=itemgetter(1))
Link back to StackOverflow question:
stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value
thank you, I have done something very similar
sorted(my_dict.getitems(), key = lambda(x): x[1])
@@coreyms sorted_x=sorted(x.values())... will this alll work to sort the values of the dictionary
@@surbhiagrawal3951 in this only values were getting sorted and not the keys whereas our requirement is to sort both keys and values together.
Can you elaborate on how you got code completion on Sublime Text for python? Thanks!
Hey There... I made a video on how I set up this Python development environment. It will show you the packages I use that do the code completions. You can find that video here:
ruclips.net/video/xFciV6Ew5r4/видео.html
Thanks!
Is there a video that explains what "class" is and what __init__ means?
Yes. If you search my channel for Python classes you’ll find my series on the subject.
@Corey Schafer
First of all thank you for the videos and the amazing explanations!!! They have been a wonder. I would like to however find out what settings or packages you use to get the info on what it is that you are typing when coding. For example in this video at 0.56 while you are typing " s_li ".... it indicates that it is a statement. I have also seen that it does this for keywords and others as well. I have watched your video in configuring sublime text for python and have added the packages that work for me and have done the settings, however those settings have not added this functionality. Could you point me in the right direction so i can configure my sublime text to allow for this? Thank you
there is a separate video where he shows how to setup Sublime text for python development. ruclips.net/video/xFciV6Ew5r4/видео.html
Hey friend! Thx for you work. Can you explain me how do you delete # symbol in SublimeText with a single click? I use mac OS and I Don't know the hotkeys to delete symbols in the few string at once