Sorting is a favorite topic in many introductory computer science classes, where they teach you dozens of different ways of how to go about sorting data. That's all well and good, and it has value from an algorithmic perspective, where you can learn about the different techniques that you can use to sort data, but it's not very practical. Why? Well, pretty much all modern programming languages, including Python, have sorting routines in their libraries. And the great thing about these sorting routines is someone else wrote them. Not only that, someone else debugged them, and someone else made them fast. So, instead of learning about sorting techniques, why don't we learn about how to use these routines instead? Let's check it out. Let's take a look at how we can sort sequences in Python, but before we can do that, we actually need something to sort. I'm kind of lazy, so let's just use range to get some numbers. So, I'm going to call range(10) here and then convert it into a list then I conveniently will have a list of numbers. It's a problem with that though. It's not very interesting to sort that because it's already sorted. So let's think about this. Oh, well, Python conveniently has a random module. So if I import random here, I have access to it. And I'm going to call random.shuffle, and what that conveniently does is randomly shuffle a list. Now, I encourage you to go read the documentation on the random module, it has many other features that might come in handy if you need to do something random like this. So this should give me some randomly shuffled numbers in a list. And let's see. Yes, it does. So I have some randomly shuffled numbers. They may be different every time, but that doesn't really matter for our purposes. Now, let's sort it. And so, as I said, in many introductory computer science courses, you'll figure out 20 different ways of doing this, all of which where you had to write code. Well, I'm not going to do that. I'm going to write one line, data.sort. And that's it. You got to love Python, right? Well, let's see if it actually works. And, lo and behold, it does. My data is back to the way it was when I originally called range, so I've unshuffled it, so to speak. That's pretty easy. That's all we need to know. We called sort. We're done. Well, let's randomly shuffle the data again and see if there's another way that we can sort it. Python also has a function called sorted. So I can call .sort on a list, or I can call sorted and pass it the list. Let's see what happens here. So my data reshuffled again. After I call sorted on data, the data remains shuffled, but sorted returned a new list where the data in the original list was sorted. So if I have a list, and I just want a sort it in place, meaning I wanted to mutate the list and just change the list so that the data inside of it is sorted, I can call the .sort method of lists. If I would like to keep my initial list intact, I can call sorted and pass it that list. It will create a new list with a copy of the original data in sorted order. Now, what if I want to sort something that isn't a list? Let's get ourselves a tuple here. If I run this, you'll see that I now have a data tuple with the data in some random order, and let's sort that. We'll call dot sort on the tuple and see what happens. Wait a minute. That didn't work. Now, you should have been able to predict that wouldn't work. I told you when you call dot sort on the list, it actually mutates the list and sorts the data inside of it. Well, tuples can't be mutated. So tuples do not have a sort method, so you cannot call dot sort on a tuple. Well, that makes sense. Does that mean we're out of luck if we have our data in a tuple and want to sort it? Well, of course not. Sorted to the rescue. Sorted will take anything that you can iterate over. So anything that you could loop over with a for loop, you can pass to sorted, and it will sort it for you and return a new list. It's not going to mutate its input, so it doesn't matter that it's a tuple. And you can see that returned a list, it's not returning a tuple even though I pass a tuple. Sorted always returns a list, and sorted returns a list with the data that was in the tuple that you passed it in sorted order. Let's just keep going here and see what else we can sort. Let's make a dictionary and see what happens. So how am I going to easily create a dictionary? Remember, I'm lazy. All right. So this is what's called a dictionary comprehension. And this is going to create a dictionary. If you look at it hard enough, you might be able to figure it out. It's enclosed in curly braces telling Python it's a dictionary, and then I have key: key** 2. That's going to map key to key squared, but what's key going to be? Well, for key in datatup is going to iterate through datatup, and for each item, they'll assign the value to key and then put in the dictionary something that maps that key to it squared. So I would expect all the data in datatup to appear as keys in this dictionary with values that are the square of the key. Let's see if that happens. All right. You can also go read the Python documentation on dictionary comprehensions. There are also lists comprehensions that allow you to make lists in a similar way as well, but I'll let you read up on the documentation to figure that out. Now, let's just call sorted on this and see if it sorts our dictionary for us. What it did was sort the keys. Now, this kind of makes sense because what I told you before is sorted takes anything you can iterate over. It's effectively iterating over the input and sorting what it gets out. And when we iterate over a dictionary directly, well, what we get out are the keys. There you have it. Sorting in Python is relatively simple. You can call the sort method if you have a list, and you want to actually mutate the list and sort the data inside of it, or you can call the sorted function with anything that you could iterate over, and it will produce a list with the items in the original input in sorted order. Now, I also exposed a little bit of my laziness here in this video, but I would prefer to think of it as my efficient use of my time. Using things like the dictionary comprehension, the random module so that I can shuffle up things in a list that I created with range, these are the tricks of the trade if you're a scripter. You don't want to write a code that you don't need to. You want to use the language to its full potential. I want to call the sort routines the language has. I want to use the random module when I can instead of randomly shuffling things in my head, and so on. I encourage you to read the documentation on these things if you're interested in finding out more, but you don't really need them for the things that we're going to do. I just want to expose you to them.