Python Program: How to Sort a List of Numbers

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 num…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Python Program: How to Sort a List of Numbers." arju10 | Sciencx - Tuesday October 29, 2024, https://www.scien.cx/2024/10/29/python-program-how-to-sort-a-list-of-numbers/
HARVARD
arju10 | Sciencx Tuesday October 29, 2024 » Python Program: How to Sort a List of Numbers., viewed ,<https://www.scien.cx/2024/10/29/python-program-how-to-sort-a-list-of-numbers/>
VANCOUVER
arju10 | Sciencx - » Python Program: How to Sort a List of Numbers. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/29/python-program-how-to-sort-a-list-of-numbers/
CHICAGO
" » Python Program: How to Sort a List of Numbers." arju10 | Sciencx - Accessed . https://www.scien.cx/2024/10/29/python-program-how-to-sort-a-list-of-numbers/
IEEE
" » Python Program: How to Sort a List of Numbers." arju10 | Sciencx [Online]. Available: https://www.scien.cx/2024/10/29/python-program-how-to-sort-a-list-of-numbers/. [Accessed: ]
rf:citation
» Python Program: How to Sort a List of Numbers | arju10 | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.