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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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