by Sai gowtham

How to sort a list of numbers in Python

This example will show you how to sort a list of numbers in python by using sort() method.

Sort numbers in ascending order

numbers = [4, 7, 1, 0, 2, 5, 3]

numbers.sort()

print(numbers)

Output:

[0, 1, 2, 3, 4, 5, 7]

Sort numbers in descending order

numbers = [4, 7, 1, 0, 2, 5, 3]

# for descending  order we need to pass `reverse=True` argument
numbers.sort(reverse=True)

print(numbers)

Output:

[7, 5, 4, 3, 2, 1, 0]

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course