Account
Categories

Python sort() Method


Definition:

If you need to arrange the values of a list, this method sorts them in order.

Note: If you do not specify anything, the list will be sorted in ascending order by default.

Now you will clearly understand the method through the flowchart .

List →  a = [30, 10, 20]

Index :   0      1      2  
          ↓      ↓      ↓
Value :  30     10     20  

Method →  a.sort()

After Sort →  [10, 20, 30]

Index :   0      1      2  
          ↓      ↓      ↓
Value :  10     20     30  

Syntax

list_name.sort()

Example:

a = [30, 10, 20]
a.sort()
print(a)

Output:

Exercise

Sort the list a = [25, 5, 15] using Python.