Figure out how to sort your data. The sorted() function allows us to sort a given iterable, where each item should be the same datatype. When given an iterable, the sorted() function returns a list with the numbers sorted from least to greatest. When given an iterable of characters or strings, the function sorts them alphabetically. You can also use key parameters to change how the list is sorted. This can be helpful when you need to organize or sort data in your Python code.
- [Instructor] We can sort data with the sorted function.…The sorted function takes one input, an iterable,…which is just something we can iterate through.…Some examples of these are lists, tuples, strings,…dictionaries, and you've already had experience…with this before.…The sorted function then outputs a list with the items…from the input sorted.…And there are also some optional parameters…that we'll get into later.…So let's start off by creating a variable,…and it's going to be called pointsInaGame.…
They are points that a series of people have gotten…in a specific game.…And so say we have zero, -10, 15, -2, and one, and 12.…Then we can sort all of the points in the game…by going sortedGame equals sorted,…and then just putting in this list,…because this list is an iterable…and so therefore it can be sorted.…Let's go ahead and print out this sorted game…and see all of these points all lined up and sorted…in order from least to greatest.…
We can also use the sorted function for alphabetical input.…So say we have a list of children,…
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content
Skill Level Intermediate
1. Python: What's Built-In?
2. Python: Math
3. Python: Input/Output
4. Python: Date & Time, HTML Parsing, and HTTP
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.