Hello guys, post your comments, views, and questions here for answers or discussions. Remember, the best comments are those that help build others, those from which everyone or at least someone can learn something new. Happy Pythoning!
Sort (). Python's sort() comes in handy when you want to sort or order a list using the magnitude or the size of it's elements both in ascending order and descending order. By default, sort() doesn't require any extra arguments. However, it has two optional parameters: reverse - If given and true, the sorted list is reversed (or sorted in Descending order) key - function that serves as a key for the sort comparison Let's look at code examples (Click in the code editor below to view code):
my_list=[3,5,1,7,2,0] my_list.sort() print(my_list) #Output [0,1,2,3,5,7] # Providing reverse argument # for descending my_list.sort(reverse=True) print(my_list) # Output [7,5,3,2,1,0]