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

 Live Demo

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.

Updated on: 2021-04-16T11:58:28+05:30

828 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements