Lesson 19 of 55
9 mins readPython Tuples & Pair Operations (fst, snd)
In Plain English
Unlike homogeneous lists, tuples have a fixed length and can contain elements of different types (e.g. (1, "Alice", True)). Pairs (2-tuples) feature helper functions fst and snd.
Deep Dive: How It Works
Tuple Types: (Int, String) is a 2-tuple. The type depends on both size and element types.
Pair Functions: fst (x, y) = x and snd (x, y) = y work exclusively on 2-tuples.
Pattern Matching: Destructure arbitrary tuples: (a, b, c) -> a + b + c.
Core Rules to Remember

Heterogeneous Types: Combine multiple different data types into a single fixed record.

fst and snd: Extract the first and second elements from 2-tuples.
Live Interactive Example
Hit Run Code to see it liveTuples and Pattern Destructuring
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
Output Console
Click "Run Code" to view the rendered output.
How it works: minMaxAvg returns a 3-tuple destructured into minVal, maxVal, avgVal.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Swap Pair Elements
Define `swapPair (a, b) = (b, a)`.
Print `"Swapped: "` followed by `show (swapPair (1, "two"))`.
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.