1# sort() will change the original list into a sorted list
2vowels = ['e', 'a', 'u', 'o', 'i']
3vowels.sort()
4# Output:
5# ['a', 'e', 'i', 'o', 'u']
6
7# sorted() will sort the list and return it while keeping the original
8sortedVowels = sorted(vowels)
9# Output:
10# ['a', 'e', 'i', 'o', 'u']
1>>> sorted(student_tuples, key=itemgetter(2))
2[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
3
1>>> student_tuples = [
2... ('john', 'A', 15),
3... ('jane', 'B', 12),
4... ('dave', 'B', 10),
5... ]
6>>> sorted(student_tuples, key=lambda student: student[2])
7# sort by age
8[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
9
1>>> x = [1 ,11, 2, 3]
2>>> y = sorted(x)
3>>> x
4[1, 11, 2, 3]
5>>> y
6[1, 2, 3, 11]
7
1>>> class Student:
2... def __init__(self, name, grade, age):
3... self.name = name
4... self.grade = grade
5... self.age = age
6... def __repr__(self):
7... return repr((self.name, self.grade, self.age))
8