To sort a list in Python, you can use the sort() method. This method modifies the original list in place and sorts its elements in ascending order.
Example:
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
my_list.sort()
print(my_list)
Output:
[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
In this example, the original list is modified in place and sorted in ascending order.
If you want to sort the list in descending order, you can pass the reverse=True argument to the sort() method:
Example:
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
my_list.sort(reverse=True)
print(my_list)
Output:
[9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]
In this example, the original list is modified in place and sorted in descending order.
You can also use the sorted() function to create a new sorted list without modifying the original list:
Example:
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_list = sorted(my_list)
print(sorted_list)
Output:
[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
In this example, a new sorted list is created from the original list, but the original list is not modified.