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
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.
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
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!
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.. :)
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
@@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 :)
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......................................
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.
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?
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'?
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.
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
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?
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
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.
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?
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
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.
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?
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
@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
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
Hie, suppose we have list of objects of class A with attributes (a1,a2,a3,...an). We want to sort this list of objects such that a1 should be maximum, a2 should be minimum, a3 should be maximum and so on. How should I proceed to write comparator function? Could you help?
+Anurag Negi I'm not sure I understand. So let's say you had [a1,a2,a3,a4,a5,a6]. Would you want this sorted in this order: [a6,a1,a5,a2,a4,a3]? Alternating?
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.
why do you have to assign function (e_sort ) to a a key. Why can't we just assign .name method to key variable to pass all employees ? is it not possible without using anonymous or regular function ? Also does they Key variable has to be called KEY or is it just convention ?
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.
Hey there. That is because the function is using a parameter named "emp". The employee object is passed into that function as an argument, and then that "emp" variable is equal to the employee object. If you watch my video on Function I go further into this concept. Hope that helps.
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!
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
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 >>>>>>>
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
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.
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?
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
Your video is the best for Python learners !!!!!!!!
Corey is teaching the things we need to know exactly. Thanks.
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...
Excellent way teaching
you're incredible at explaining stuff man
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
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!
nice place to go back to the basics and a great road for beginners
Who would have thought sorting things was so easy! Thanks man, this is gonna be useful =D
Good bless you
and God bless your courses
learning to get my goal to financially independent
and work in space company and start-ups!
Omg I have learned so much about this basic topic even tho I'm working whit classes!!
the reason they removed the dislike button was because they did not want people to dislike this video because its so good
7:07
clearest explanation of #repr
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 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
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
@4:45
3 lines of code to do what I've been struggling to do with 12-18 lines. Good work, sir.
I was trying to do this exact thing just today. so glad I stumbled on this video. thanks!
For 8:01 what if I wanted to put input values for the name age and salary how will I do that?
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 :)
Thanks mr. Corey Schafer, very useful as usual!
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......................................
Very clear, as always.
Corey, Very nice and thanks for sharing.
Thank you very much, mate! This video really helped me!
Thank you Corey for your time and effort. Keep spreading the knowledge.
you are the best, you help me a lot TNX.
you are amazing, thanks for these awesome videos
thankyou, you help me for PTI-B homework
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
Such an amazing tutorial! Keep up the good work brother!
you saved my life
Video was head on. Thank you very much.
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?
Awesome video 🎉
Key=Custom function -> lambda function->attrgettrer->itemgetter
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
as usual great tutorial, hopefully I can finish the whole series up to date!
Great video. Thank you!!
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
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!!
Your videos are awesome really loved them . please add videos on some popular libraries like pygame,openpyxl etc.
thank you so much :)... very informative videos
God bless you Corey!!!
you are just awesome man
Hey, at last you use the attrgetter method so what's the difference between the attrgetter and itemgetter
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
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?
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
on the function "e_sort()", how does salary, age or name is returned from the class?
Amazing vid
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
Thank you!
sorry i wanna ask, can i sorting value of list without change an index
If you want name as primary sort and then age as secondary sort...how would you do that?
thank you :)
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?
Absolutely amazing
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 .
I am wondering if you should create a companion video for multiple key sort.
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
good info. thanks.
This is very helpful, thank you!
How do you match or intersection two multidimensional list of a list
More power by the way.
thanks bro!
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.
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.
Great vid. How about a video on linked-list vs. arrays, and sort algorithms?
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?
Thanks you so much
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
@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
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
Thank you
Hie, suppose we have list of objects of class A with attributes (a1,a2,a3,...an). We want to sort this list of objects such that a1 should be maximum, a2 should be minimum, a3 should be maximum and so on. How should I proceed to write comparator function? Could you help?
+Anurag Negi I'm not sure I understand. So let's say you had [a1,a2,a3,a4,a5,a6]. Would you want this sorted in this order: [a6,a1,a5,a2,a4,a3]? Alternating?
Hey @corey, is this formatting still valid in the latest python?
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.
what text editor you are using? Looks cool
Bill Deng likely sublime, or vs code, or atom
I have been following the python playlist and I am slightly confused by what was the use of the class here?
why do you have to assign function (e_sort ) to a a key. Why can't we just assign .name method to key variable to pass all employees ? is it not possible without using anonymous or regular function ?
Also does they Key variable has to be called KEY or is it just convention ?
How can we sort using earliest and latest datetime data
Pls do reply..thanks
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...
What is dont understand is, how " e_sort" is linking to employees table.? We are not passing any argument in e_sort function.
So with sorting objects what if the age is the same integer value, how can we switch it to sort based on the name alphabetically
How can you use emp.name instead of employee.name when your class name is employee?
How does it return the emp.name??
Confused!!!
Hey there. That is because the function is using a parameter named "emp". The employee object is passed into that function as an argument, and then that "emp" variable is equal to the employee object. If you watch my video on Function I go further into this concept. Hope that helps.
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!
Hi, thank you for all your videos. How do you comments out a line or several lines?
ctrl + /
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.
Hey ur videos r awesome can u make videos on data structure and algorithm
can any one help me with out using built in functions can we write a code for ascending or descending order of dictionary based by values
Can I replace e_sort after key as self.name?