This content originally appeared on DEV Community and was authored by arju10
List:
A list is a collection of items in python, created using square brackets [] . List can contain different data types, including numbers, strings and other lists. List is mutable ( It can't changed/modified).
Sort:
In list very easily numbers can be sorted.
Prerequisites for Sorting a List of Numbers:
Before starting sort the list, you need to know about List & How to create a list.
Create a List
In the beginning, We will create a list
# Create a list with Numbers
my_list = [10,80,20,60,30,40,70]
# Print the list
print("List: ", my_list)
Here, 10
, 80
, 20
, 60
, 30
, 40
and 70
are Numbers or we can call integer type data.
We will store these numbers in my_list
variable.
print()
is a builtin function in python which used to print or show the result whatever we want.
Multiple Ways to Sort List of Numbers or Integers
sorted() is used for sort a list in ascending order & can be descending order by setting the reverse parameter to True.
Method 1: Using the builtin sorted() function
This content originally appeared on DEV Community and was authored by arju10
arju10 | Sciencx (2024-10-29T09:20:11+00:00) Python Program: How to Sort a List of Numbers. Retrieved from https://www.scien.cx/2024/10/29/python-program-how-to-sort-a-list-of-numbers/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.