How to sort a list of numbers in Python
In this post we will give you information about How to sort a list of numbers in Python. Hear we will give you detail about How to sort a list of numbers in PythonAnd how to use it also give you demo for it if it is necessary.
we are going to learn about how to sort the list of numbers in ascending & descending order in Python
Sorting list of numbers in ascending order
To sort the list of numbers in ascending order, we can use Python’s built-in sort() method.
Here is an example, that sorts the numbers list to ascending order:
numbers = [4, 7, 1, 0, 2, 5, 3]numbers.sort()print(numbers)
Output:
[0, 1, 2, 3, 4, 5, 7]
Sorting list of numbers in descending order
To sort the list of numbers in descending order, we can use the built-in sort() method by passing reverse=True as an argument to it.
Here is an example, that sorts the numbers list to descending order:
numbers = [4, 7, 1, 0, 2, 5, 3]numbers.sort(reverse=True)print(numbers)
Output:
[7, 5, 4, 3, 2, 1, 0]
Hope this code and post will helped you for implement How to sort a list of numbers in Python. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs