Python Program for Sort List in Ascending and Descending Order

Python Program for Sort List in Ascending and Descending Order

Python program for sort list in ascending and descending order; In this python tutorial, we would love to share with you how to sort the elements of a list in Ascending and Descending order in Python.

Use the python inbuilt method name sort(), which is used to sort the elements/objects of the list in Ascending and Descending Order.

Basic Syntax of sort method:

 list.sort()

Python Program for Sort List in Ascending and Descending Order

  • Python Program to Sort List Elements in Ascending Order
  • Python Program to Sort List Elements in Descending Order

Python Program to Sort List Elements in Ascending Order

# List of integers
num = [100, 200, 500, 600, 300]

# sorting and printing 
num.sort()

#print
print(num)

# List of float numbers
fnum = [100.43, 50.72, 90.65, 16.00, 04.41]

# sorting and printing
fnum.sort()

#print
print(fnum)

# List of strings 
str = ["Test", "My", "Word", "Tag", "Has"]

# sorting and  printing
str.sort()

#print
print(str)

After executing the python program, the output will be:

[100, 200, 300, 500, 600]
[4.41, 16.0, 50.72, 90.65, 100.43]
['Has', 'My', 'Tag', 'Test', 'Word']

As you know above, how to sort list elements in ascending order. Now you will read how to sort a list in descending order by using sort() method.

You pass reverse=True as an argument with sort() method to sort a list elements in descending order.

You can see the following program to sort a list element in descending order.

Python Program to Sort List Elements in Descending Order

# List of integers
num = [100, 200, 500, 600, 300]

# sorting and printing 
num.sort(reverse=True)

#print
print(num)

# List of float numbers
fnum = [100.43, 50.72, 90.65, 16.00, 04.41]

# sorting and printing
fnum.sort(reverse=True)

#print
print(fnum)

# List of strings 
str = ["Test", "My", "Word", "Tag", "Has"]

# sorting and  printing
str.sort(reverse=True)

#print
print(str)

After executing the program, the output will be:

[600, 500, 300, 200, 100] 
[100.43, 90.65, 50.72, 16.0, 4.41] 
['Word', 'Test', 'Tag', 'My', 'Has']

Recommended Python String Programs

  1. Python Program to Convert Uppercase to Lowercase
  2. Convert String Lowercase to Uppercase in Python
  3. Python First Character of String Uppercase
  4. Python Concatenate String and Variable (int, float, etc)
  5. How to Find Length of String in Python
  6. Python: String Join Function
  7. Python String Append Examples
  8. How to replace a character in a string in python
  9. Python – Count Number of Occurrences in String
  10. Python Remove Last Character from String
  11. Python Remove Character From String
  12. Python Program to Reverse String
  13. Python Program Count vowels in String
  14. Python Split a String into Array of Characters, Space
  15. Python Program to Swap Two Character of Given String
  16. Python Program String Find
  17. Python: Two Given Strings and Swap First Two Characters of Each String
  18. Print All Permutations of Given String in Python
  19. Python | Check Whether a String Contains Number or Letter
  20. Python: Convert String to Float & Float to String
  21. Python Program to Reverse String Using Stack
  22. How To Convert Python Int to String and String to Int
  23. Python Convert String to List and List to String

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *