liveBook · Manning

Tuple

As explained in Python Workout: 50 ten-minute exercises

A tuple is an ordered, finite Python sequence that is commonly immutable and can contain elements of different types.

A tuple is an ordered, immutable Python sequence. Use it for a fixed record whose fields may have different types, such as a person’s name and birth date; use a list for a changeable sequence of similar items. section 3.0 section 3.26

Work with tuples using the same core sequence operations used for lists:

  • Retrieve fields with numeric indexes or slices, iterate with for loops, and search with in. section 3.0
  • Avoid changing tuple contents; add or remove records by modifying an enclosing list instead. section 3.0
  • Use sorted with a key function when ordering tuples; sorted returns a list. section 3.26
  • Unpack tuples into named variables, as enumerate does with each index-and-item pair. section 1.18

Accepting any number of arguments

def mysum(*numbers): output = 0 for number in numbers: output += number return output mysum(*[1, 2, 3]) # 6 The *numbers parameter always receives a tuple, including () when no arguments are supplied. At the call site, * expands a list into separate arguments; without it, the list becomes the tuple’s single element. section 1.8

Formatting tuple records

people = [('Jones', 'Ada', 12.5), ('Smith', 'Bo', 9.75)] for person in sorted(people): print('{1:10} {0:10} {2:5.2f}'.format(*person)) Each iteration retrieves one tuple record. Passing *person supplies its three fields as separate arguments to str.format, while tuple ordering supports alphabetical sorting. section 3.22

Table 3.1 Sequence comparison (view table figure)

Type

Mutable?

Contains

Syntax

Retrieval

str

No

One-element strings

s = 'abc'

s[0] # returns 'a'

list

Yes

Any Python type

mylist = [10, 20, 30, 40, 50]

mylist[2] # returns 30

tuple

No

Any Python type

t = (100, 200, 300, 400, 500)

t[3] # returns 400

Compare tuple mutability, accepted element types, syntax, and indexed retrieval with the corresponding properties of strings and lists. section 3.0

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains immutability, sequence operations, unpacking, sorting, slicing, and splat expansion through worked Python examples.
Examples
Uses variable-argument summation, sorted person records, enumerate pairs, and comment-line filtering with startswith.
What it assumes
Assumes basic Python functions and sequences while teaching tuple selection, indexing, iteration, unpacking, and immutability.
What you can do after
Apply it via Python sequence operations, argument unpacking, tuple-record sorting, and variable-argument functions.
How it connects
Connects tuples with lists, sequences, slicing, iterable argument expansion, unpacking, dictionaries, and database records.

Also explained in

Compare how 15 books explain tuple →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.