Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 8: Tuples in Python

A tuple is an ordered sequence of elements, similar to a list. However, unlike lists, tuples are immutable. Once a tuple is created, its elements cannot be changed, added, or removed. Tuples are defined by enclosing elements in parentheses ().

8.1 Introduction and Indexing

Tuples are often used to store heterogeneous data (different data types) and write-protected data.

t = (1, "Apple", 3.14)
print(t[1]) # Output: Apple

Note: To create a tuple with a single element, you must include a trailing comma, otherwise Python interprets it as an expression inside parentheses.

single_t = (5,)  # This is a tuple
not_a_t = (5)    # This is an integer

8.2 Tuple Operations

Because they are immutable, tuples only support operations that do not modify them.

  • Concatenation (+): (1, 2) + (3, 4) results in (1, 2, 3, 4).
  • Repetition (*): (1,) * 3 results in (1, 1, 1).
  • Membership (in, not in): Checks existence.
  • Slicing: Extracts a sub-tuple. t[1:3]

8.3 Tuple Assignment

Python supports a powerful feature called tuple assignment (or tuple unpacking). It allows you to assign values to multiple variables simultaneously.

# Packing
t = (10, 20, 30)

# Unpacking
a, b, c = t
print(a) # Output: 10
print(b) # Output: 20

This also makes swapping variables incredibly easy:

x, y = 5, 10
x, y = y, x  # Swaps values!

8.4 Built-in Functions and Methods

Since tuples are immutable, they lack methods like append, remove, or sort. However, many built-in functions work with tuples.

  • len(tuple): Returns number of elements.
  • tuple(sequence): Converts a sequence to a tuple.
  • count(item): Returns the frequency of item in the tuple.
  • index(item): Returns the index of the first occurrence of item.
  • min(tuple) / max(tuple) / sum(tuple): Finds minimum, maximum, and sum of numeric tuples.
  • sorted(tuple): Returns a new sorted list from the tuple elements (does not modify the original tuple, and returns a list, not a tuple).

8.5 Nested Tuples

Tuples can contain other tuples (or lists).

nested_t = ((1, 2), (3, 4))

Competency Based Questions

Q1. Predict the Output What will be the output of the following code?

t1 = (10, 20, 30)
t2 = (40, 50)
t3 = t1 + t2
print(t3 * 2)

Q2. Assertion-Reasoning

  • Assertion (A): The code T = (10, 20); T[0] = 50 will result in a TypeError.
  • Reason (R): Tuples are immutable, so item assignment is not supported. Choose the correct option: a) Both A and R are true and R is the correct explanation of A. b) Both A and R are true but R is NOT the correct explanation of A. c) A is true but R is false. d) A is false but R is true.

Q3. Case-Based Scenario Rohit needs to store the coordinates (Latitude and Longitude) of a specific geographical location in his program. These coordinates must never change during the execution of the program.

  1. Which data structure (List or Tuple) should Rohit use and why?
  2. Write the code to store Latitude 28.7041 and Longitude 77.1025 in the chosen data structure.

Q4. Application-Oriented Write a Python program to input a tuple of numbers and find the maximum and minimum values using built-in functions.


Answers to Competency Based Questions

A1. (10, 20, 30, 40, 50, 10, 20, 30, 40, 50) Explanation: t1 + t2 concatenates to (10, 20, 30, 40, 50). Multiplying by 2 repeats the entire tuple twice.

A2. a) Both A and R are true and R is the correct explanation of A. Because tuples are immutable, you cannot change the value at a specific index once it has been created.

A3.

  1. Rohit should use a Tuple. Since the coordinates should never change during execution, an immutable data structure ensures the data remains write-protected and prevents accidental modifications.
  2. coordinates = (28.7041, 77.1025)

A4.

# Assuming the user inputs numbers separated by commas: 5, 2, 9, 1
user_input = input("Enter numbers separated by commas: ")

# Convert input string to a tuple of integers
# Using a generator expression with tuple()
t = tuple(int(x) for x in user_input.split(','))

print("Maximum value is:", max(t))
print("Minimum value is:", min(t))