Sorting in Python

The sort() method is a built-in Python method that, by default, sorts the list in ascending order. However, you can modify the order from ascending to descending by specifying the sorting criteria.

Example of Sort

Let's say you want to sort the element in prices in ascending order. You would type prices followed by a . (period) followed by the method name, i.e., sort including the parentheses.

Example of Printing Sorted Lists

Again, using a similar example as above, we will use the .sort() method to sort and print the prices in the list named prices.

[37.13, 71.17, 159.54]

Try it for yourself.

To learn more about sorting in Python, please see this video from our course Introduction to Python for Finance.

This content was taken from DataCamp’s Introduction to Python for Finance course by Adina Howe.