Python Program: How to Sort a List of Numbers



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