home
0%
Progress
StudentPortal › Class XI - CS › Unit 2
CBSE Code 083 | Unit 2: Computational Thinking & Programming

07. Tuples in Python

Tuple creation, immutability, packing/unpacking, nested tuples, tuple vs list comparison.

Unit 2: Computational Thinking & Programming

Chapter 7: Tuples in Python

1. Introduction to Tuples

A Tuple is an ordered, immutable sequence of heterogeneous elements enclosed in parentheses ().

Creating Tuples

t1 = ()                     # Empty tuple
t2 = (10, 20, 30)           # Standard tuple
t3 = (10, "Hello", 3.14)    # Heterogeneous tuple
t4 = 10, 20, 30             # Tuple packing without parentheses

Crucial CBSE Exam Rule for Single Element Tuples: t = (5) is an integer (int), NOT a tuple!
To create a tuple with one element, a trailing comma is mandatory: python t = (5,) # Type: tuple print(type(t)) # <class 'tuple'>


2. Immutability of Tuples

Once a tuple is initialized, its elements cannot be reassigned, inserted, or removed:

t = (1, 2, 3)
t[0] = 99  # TypeError: 'tuple' object does not support item assignment

Mutability Inside Tuples

If a tuple contains a mutable object (such as a list), the internal elements of that list can be modified:

t = (10, [20, 30], 40)
t[1].append(999)  # Legal!
print(t)          # (10, [20, 30, 999], 40)

3. Tuple Operations and Slicing

Tuples support indexing and slicing identically to strings and lists:

t = (10, 20, 30, 40, 50)
print(t[1:4])   # (20, 30, 40)
print(t[::-1])  # (50, 40, 30, 20, 10)

# Concatenation and Replication
t_double = t + (60, 70)
print(t_double) # (10, 20, 30, 40, 50, 60, 70)

rep = (1, 2) * 3
print(rep)      # (1, 2, 1, 2, 1, 2)

4. Built-in Tuple Functions

Function Purpose Example Result
len(T) Total number of elements. len((1, 2, 3)) 3
T.count(x) Number of times value x appears. (1, 2, 1, 1).count(1) 3
T.index(x) Index of first occurrence of x. ('a', 'b', 'c').index('b') 1
min(T) Smallest element. min((45, 12, 89)) 12
max(T) Largest element. max((45, 12, 89)) 89
sum(T) Sum of numeric elements. sum((10, 20, 30)) 60
sorted(T) Returns a list with sorted elements. sorted((3, 1, 2)) [1, 2, 3]
tuple(seq) Converts sequence to a tuple. tuple("ABC") ('A', 'B', 'C')

5. Tuple Packing and Unpacking

  • Packing: Bundling multiple values into a single tuple. record = ("Aarav", 11, "Science")
  • Unpacking: Extracting tuple items into individual variables: name, grade, stream = record (Number of variables on the left must exactly match number of elements in tuple).
# Swapping two variables using tuple packing/unpacking
x, y = 10, 20
x, y = y, x  # Seamless Python swap!
print(x, y)  # 20 10

6. Tuples vs. Lists Comparison

Feature List (list) Tuple (tuple)
Syntax Square brackets [] Parentheses ()
Mutability Mutable (modifiable in-place) Immutable (read-only)
Memory & Speed Higher memory overhead; slower iteration Lower memory footprint; faster access
Dictionary Keys Cannot be used as dictionary keys Can be used as dict keys (if elements are immutable)
Use Case Collections of data expected to change Fixed constants, database records, coordinate pairs

7. Board Exam Practice Questions & Solutions

Q1. Predict the output of the following code:

t = (10, 20, 30)
a, b, c = t
print(a + b * c)

val = (40)
print(type(val))

Answer:

610
<class 'int'>

Explanation: b * c = 20 * 30 = 600; a + 600 = 610. (40) without a comma is treated as an integer in parentheses, so its type is int.

Q2. Write a Python function that takes a tuple of numbers and returns a tuple containing the minimum, maximum, and average. Answer:

def compute_stats(t):
    min_val = min(t)
    max_val = max(t)
    avg_val = sum(t) / len(t)
    return (min_val, max_val, avg_val)

numbers = (14, 25, 8, 42, 31)
print(compute_stats(numbers))  # (8, 42, 24.0)

Entity Details

Create New Item

help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.