Lesson 19 of 55
9 mins readPython Tuples & NamedTuples
In Plain English
Tuples are lightweight, immutable sequences that allow returning multiple values from functions with zero allocation overhead. NamedTuples add named field access.
Deep Dive: How It Works
Tuples: (1, 2, "three") creates a tuple. Elements cannot be mutated after creation. Single-element tuples require a trailing comma: (1,).
Destructuring: a, b = (10, 20) unpacks tuple elements into separate variables.
NamedTuples: (name="Julia", version=1.10, speed="Fast") provides dot access (nt.name) while retaining stack-allocated immutability.
Core Rules to Remember

Zero Allocation Immutability: Tuples are allocated on the stack by the Julia compiler for ultra-fast access.

NamedTuples: Combine the speed of tuples with the clean field syntax of structs.
Live Interactive Example
Hit Run Code to see it liveTuples and NamedTuples
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: Destructuring unpacks the 3 values into min_val, max_val, avg_val.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Construct a Point NamedTuple
Create a NamedTuple `point = (x=3.5, y=7.2)`.
Print `"Point: x=$(point.x), y=$(point.y)"`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.