Home >Backend Development >Python Tutorial >Python Program: How to Sort a List of Numbers

Python Program: How to Sort a List of Numbers

Linda Hamilton
Linda HamiltonOriginal
2024-11-02 18:15:291048browse

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



The above is the detailed content of Python Program: How to Sort a List of Numbers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn