The essentials
Quick reference
One focused task per row. Jump to the related section for complete, working examples.
| Use | Syntax | Examples |
|---|---|---|
| Create a tuple | point = (3, 4) | View examples |
| One-item tuple | single = ('only',) | View examples |
| Pack values | record = name, score, active | View examples |
| Unpack positions | name, score = record | View examples |
| Capture the middle | first, *middle, last = values | View examples |
| Count from zero | range(5) | View examples |
| Use start, stop, step | range(10, 0, -2) | View examples |
| Read by index | last = values[-1] | View examples |
| Slice a sequence | copy = values[start:stop:step] | View examples |
| Test membership | if target in values: handle(target) | View examples |
| Iterate with positions | for index, value in enumerate(values, start=1): print(index, value) | View examples |
| Pair sequences strictly | pairs = zip(names, scores, strict=True) | View examples |
Tuples represent fixed-position records and ranges represent arithmetic progressions without storing every integer. Both implement the common sequence protocol, so indexing, slicing, membership, iteration, and comparison rules transfer across many built-in types.
Step by step
Detailed examples
Use tuples for fixed-shape values
The comma creates a tuple; parentheses primarily group syntax. Tuples cannot replace or append items, but they may contain mutable objects. Hashable tuples can be dictionary keys when every member is hashable.
point = (3, 4)
single = ('only',)
empty = ()
print(point[0], point[-1])
print(len(single), len(empty)) 3 4
1 0Make expected shape explicit with unpacking
Unpacking requires the item count to match unless one starred target absorbs the remainder. Swapping uses packing and unpacking without a temporary variable. Do not silently unpack records whose schema can change without validation.
values = ('start', 10, 20, 'end')
first, *middle, last = values
left, right = 1, 2
left, right = right, left
print(first, middle, last)
print(left, right) start [10, 20] end
2 1Represent integer progressions compactly
range excludes stop and stores only start, stop, and step, so even a huge range uses constant-size metadata. A zero step is invalid. Membership and indexing are arithmetic rather than a scan, and slicing returns another range.
numbers = range(10, 0, -2)
print(list(numbers))
print(numbers[1])
print(6 in numbers)
print(list(numbers[1:3])) [10, 8, 6, 4, 2]
8
True
[8, 6]Transfer indexing and slicing rules across sequences
Indexes start at zero and negative indexes count backward. Slices exclude stop and tolerate bounds beyond the sequence. Concatenation and repetition create new immutable sequences but repeated nested mutable values still refer to the same objects.
letters = ('a', 'b', 'c', 'd', 'e')
print(letters[1:4])
print(letters[::-1])
print(letters.count('b'))
print(letters.index('d')) ('b', 'c', 'd')
('e', 'd', 'c', 'b', 'a')
1
3Pair and compare with explicit assumptions
enumerate avoids a manual counter. zip stops at the shortest input by default; strict=True turns mismatched lengths into an error. Sequence comparison is lexicographic and requires corresponding unequal values to support ordering.
names = ('Ada', 'Lin')
scores = (98, 91)
for position, (name, score) in enumerate(zip(names, scores, strict=True), start=1):
print(position, name, score)
print((1, 9) < (2, 0)) 1 Ada 98
2 Lin 91
TrueLocal code tester
Explore immutable sequences
Edit tuples and ranges, then practice slicing, unpacking, enumerate, and strict zip.
Press Run to load Python locally.
Sources and further reading
References
Authoritative documentation used to verify and expand this cheat sheet.
Help us improve
Found a typo or missing example?
Tell us what would make this cheat sheet clearer, more complete, or more useful.



