3.1.5.4 SECTION SUMMARY
Key takeaways
1. You can use the
sort()
method to sort elements of a list, e.g.:lst = [5, 3, 1, 2, 4]
print(lst)
lst.sort()
print(lst) # outputs: [1, 2, 3, 4, 5]
2. There is also a list method called
reverse()
, which you can use to reverse the list, e.g.:lst = [5, 3, 1, 2, 4]
print(lst)
lst.reverse()
print(lst) # outputs: [4, 2, 1, 3, 5]
Exercise 1
What is the output of the following snippet?
lst = ["D", "F", "A", "Z"]
lst.sort()
print(lst)
Exercise 2
What is the output of the following snippet?
a = 3
b = 1
c = 2
lst = [a, c, b]
lst.sort()
print(lst)
Exercise 3
What is the output of the following snippet?
a = "A"
b = "B"
c = "C"
d = " "
lst = [a, b, c, d]
lst.reverse()
print(lst)
Comments
Post a Comment