Lists, tuples and sets cheat sheet
Collections of values. Each example shows the code and exactly what it prints.
a = [3, 1, 2] a.append(5) print(a, len(a)) | [3, 1, 2, 5] 4 | Add to the end |
a = [3, 1, 2] print(sorted(a), a) | [1, 2, 3] [3, 1, 2] | Sorted copy |
a = [3, 1, 2] a.sort(reverse=True) print(a) | [3, 2, 1] | Sort in place |
a = [10, 20, 30, 40] print(a[1:3], a[-1]) | [20, 30] 40 | Slicing |
a = [1, 2, 3] print(sum(a), max(a), min(a)) | 6 3 1 | Sum, largest, smallest |
print([x * x for x in range(5)]) | [0, 1, 4, 9, 16] | List comprehension |
a = [1, 2, 3] print(a.pop(), a) | 3 [1, 2] | Remove the last item |
t = (4, 5) x, y = t print(x + y) | 9 | Tuple unpacking |
s = {1, 2, 2, 3}
print(s, 2 in s) | {1, 2, 3} True | A set removes duplicates |
print({1, 2, 3} & {2, 3, 4}, {1, 2} | {5}) | {2, 3} {1, 2, 5} | Intersection and union |
Py-CRAFT cheat sheet. © 2026 Dr. Saptarshi Das (PhD, IIT Kharagpur | ME, Jadavpur University). All rights reserved.