The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
Create a tuplepoint = (3, 4)View examples
One-item tuplesingle = ('only',)View examples
Pack valuesrecord = name, score, activeView examples
Unpack positionsname, score = recordView examples
Capture the middlefirst, *middle, last = valuesView examples
Count from zerorange(5)View examples
Use start, stop, steprange(10, 0, -2)View examples
Read by indexlast = values[-1]View examples
Slice a sequencecopy = values[start:stop:step]View examples
Test membershipif target in values: handle(target)View examples
Iterate with positionsfor index, value in enumerate(values, start=1): print(index, value)View examples
Pair sequences strictlypairs = 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

01

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.

Fixed coordinate values
point = (3, 4)
single = ('only',)
empty = ()
print(point[0], point[-1])
print(len(single), len(empty))
Output
3 4
1 0
Back to quick reference ↑
02

Make 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.

Unpack fixed ends and variable middle
values = ('start', 10, 20, 'end')
first, *middle, last = values
left, right = 1, 2
left, right = right, left
print(first, middle, last)
print(left, right)
Output
start [10, 20] end
2 1
Back to quick reference ↑
03

Represent 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.

Inspect a descending range
numbers = range(10, 0, -2)
print(list(numbers))
print(numbers[1])
print(6 in numbers)
print(list(numbers[1:3]))
Output
[10, 8, 6, 4, 2]
8
True
[8, 6]
Back to quick reference ↑
04

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.

Slice without mutating the source
letters = ('a', 'b', 'c', 'd', 'e')
print(letters[1:4])
print(letters[::-1])
print(letters.count('b'))
print(letters.index('d'))
Output
('b', 'c', 'd')
('e', 'd', 'c', 'b', 'a')
1
3
Back to quick reference ↑
05

Pair 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.

Enumerate and strictly pair records
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))
Output
1 Ada 98
2 Lin 91
True
Back to quick reference ↑

Local code tester

Explore immutable sequences

Edit tuples and ranges, then practice slicing, unpacking, enumerate, and strict zip.

Runs in your browser
Output
Press Run to load Python locally.

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. Python Software FoundationBuilt-in Types: Sequence Typesdocs.python.org
  2. Python Software FoundationPython Tutorial: Tuples and Sequencesdocs.python.org
  3. Python Software FoundationBuilt-in Functionsdocs.python.org

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback