## Sorting Alphabetically
fruits = ["banana", "apple", "cherry", "mango"]
print("Original list:", fruits) #output: ['banana', 'apple', 'cherry', 'mango']
fruits.sort()
print("After sort (A-Z):", fruits) #output: ['apple', 'banana', 'cherry', 'mango']
fruits.sort(reverse=True)
print("After sort (Z-A):", fruits) #output: ['mango', 'cherry', 'banana', 'apple']
fruits.reverse()
print("After reverse:", fruits) #output: ['apple', 'banana', 'cherry', 'mango']
## Sorting Numerically
numbers = [5, 2, 9, 1, 7]
print("\nOriginal numbers:", numbers) #output: [5, 2, 9, 1, 7]
numbers.sort()
print("After sort (ascending):", numbers) #output: [1, 2, 5, 7, 9]
numbers.sort(reverse=True)
print("After sort (descending):", numbers) #output: [9, 7, 5, 2, 1]
numbers.reverse()
print("After reverse:", numbers) #output: [1, 2, 5, 7, 9]
## Mixing Strings and Numbers
mixed = ["apple", 3, "banana", 1]
print("\nMixed list:", mixed) #output: ['apple', 3, 'banana', 1]
# Trying to sort will raise an error
try:
mixed.sort()
except TypeError as e:
print("Error when sorting mixed list:", e)