Cheat Sheets
Tuples · Study material
Tuples Cheat Sheet
| Code | Result / meaning |
|---|---|
t = (1, 2, 3) or t = 1, 2, 3 | a tuple |
(5,) vs (5) | tuple vs int |
tuple("ab"), tuple([1, 2]) | ('a', 'b'), (1, 2) |
t[0], t[-1], t[1:] | 1, 3, (2, 3) |
t + (4,), t * 2 | new tuples |
t.count(2), t.index(3) | 1, 2 |
a, b, c = t | unpacking |
first, *rest = t | 1, [2, 3] |
a, b = b, a | swap |
return x, y | return a tuple |
d[(r, c)] = v | tuple as dict key |
tuple(sorted(t)) | sorted tuple |
namedtuple("P", "x y") | named fields |