Language

Data Structures · Lesson 9 of 56

Tuples

Source: 3-Data Structures/3.2-Tuples.ipynb

Start here — no coding background needed

What you will learn

Use fixed pairs or records that should not change accidentally.

In simple words

Tuples use `()` and are like sealed lists — good for coordinates, (name, age) pairs.

Think of it like this

A printed ID card: name and roll number fixed for the term.

Words to know:

  • tuple — Ordered, unchangeable group in parentheses

Ways to store many values together — shopping lists, contacts, unique items.

Easy example — try this first

Easy example — run this first. Change values and press Run again.

Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference notes (from full bootcamp)

Optional — deeper detail for when you are ready

Tuples

Video Outline:

  1. Introduction to Tuples
  2. Creating Tuples
  3. Accessing Tuple Elements
  4. Tuple Operations
  5. Immutable Nature of Tuples
  6. Tuple Methods
  7. Packing and Unpacking Tuples
  8. Nested Tuples
  9. Practical Examples and Common Errors
Introduction to Tuples

Explanation:

Tuples are ordered collections of items that are immutable.

They are similar to lists, but their immutability makes them different.

Reference example
Python
Output
Expected (from notebook):
()
<class 'tuple'>

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
<class 'list'>
<class 'tuple'>

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(1, 2, 3, 4, 5, 6)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[1, 2, 3, 4, 5, 6]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(1, 'Hello World', 3.14, True)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(1, 2, 3, 4, 5, 6)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
3
6

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(1, 2, 3, 4)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(6, 5, 4, 3, 2, 1)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(1, 2, 3, 4, 5, 6, 1, 'Hello World', 3.14, True)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(1,
 'Hello World',
 3.14,
 True,
 1,
 'Hello World',
 3.14,
 True,
 1,
 'Hello World',
 3.14,
 True)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[1, 2, 3, 4, 5]
[1, 'Anshul', 3, 4, 5]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(1, 2, 3, 4, 5, 6)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
1
2

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(1, 'Hello', 3.14)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
1
Hello
3.14

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
1
[2, 3, 4, 5]
6

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[1, 2, 3]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(1, 'Hello', 3.14)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
(1, 2, 3)
c

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
1 2 3 
a b c 
True False 

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Conclusion

Tuples are versatile and useful in many real-world scenarios where an immutable and ordered collection of items is required. They are commonly used in data structures, function arguments and return values, and as dictionary keys. Understanding how to leverage tuples effectively can improve the efficiency and readability of your Python code.

Practice test — try yourself

Write code, press Check. Wrong answer shows the correct code to copy & run.

t = ("Python", 3). Print the first item of the tuple.

Python