Computer >> Computer tutorials >  >> Programming >> Python

Python program to sort the elements of an array in ascending order


When it is required to sort the elements of an array in ascending order, the ‘sort’ method can be used. It helps sort the elements in ascending order by default. If we want it to be sorted in descending order, a parameter named ‘reverse’ can be set to True.

Below is a demonstration of the same −

Example

my_list = [44, 56, 42, 31, 11, 23, 78, 89, 9, 0]
print("The list is :")
print(my_list)
my_list.sort()
print("The list after sorting is :")
print(my_list)

Output

The list is :
[44, 56, 42, 31, 11, 23, 78, 89, 9, 0]
The list after sorting is :
[0, 9, 11, 23, 31, 42, 44, 56, 78, 89]

Explanation

  • A list is defined, and is displayed on the console.

  • The ‘sort’ method is called on the list.

  • The output is displayed on the console.