write a python programme to input n integers in a list sorted (in ascending or descending order ) or not .Print sorted or unsorted
Answers
Answer:
This function can be used to sort list of integers, floating point number, string and others.
# List of Integers
numbers = [1, 3, 4, 2]
# Sorting list of Integers
numbers.sort()
print(numbers)
# List of Floating point numbers
decimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]
# Sorting list of Floating point numbers
decimalnumber.sort()
print(decimalnumber)
# List of strings
words = ["Geeks", "For", "Geeks"]
# Sorting list of strings
words.sort()
print(words)
Output:
[1, 2, 3, 4]
[1.68, 2.0, 2.01, 3.28, 3.67]
['For', 'Geeks', 'Geeks']
To sort the list in descending order.
Syntax
list_name.sort(reverse=True)
This will sort the given list in descending order.
# List of Integers
numbers = [1, 3, 4, 2]
# Sorting list of Integers
numbers.sort(reverse=True)
print(numbers)
# List of Floating point numbers
decimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]
# Sorting list of Floating point numbers
decimalnumber.sort(reverse=True)
print(decimalnumber)
# List of strings
words = ["Geeks", "For", "Geeks"]
# Sorting list of strings
words.sort(reverse=True)
print(words)
Output:
[4, 3, 2, 1]
[3.67, 3.28, 2.01, 2.0, 1.68]
['Geeks', 'Geeks', 'For']
Syntax :
list_name.sort() – it sorts in ascending order
list_name.sort(reverse=True) – it sorts in descending order
list_name.sort(key=…, reverse=…) – it sorts according to user’s choice
Explanation:
please Mark me as brainliest