Tuples in Python

Tuples in Python (Class 11)

1. Introduction to Tuples

A tuple is an ordered, immutable (cannot be changed), and indexed collection of items in Python.
It is written using round brackets ().

Example

t = (10, 20, 30, "Hello", 3.14)

Key Features of Tuples

  • Ordered: Items have a fixed order.
  • Immutable: Cannot add, change, or remove elements.
  • Allow duplicates.
  • Can store different data types.

2. Creating Tuples

(a) Normal Tuple

t = (1, 2, 3)

(b) Tuple Without Parentheses (tuple packing)

t = 10, 20, 30   # packing

(c) Empty Tuple

t = ()

(d) Single Element Tuple

A tuple with one element must have a trailing comma:

t = (5,)     # single element tuple

3. Indexing in Tuples

Indexing allows us to access elements by their position.

Positive Indexing

Starts from 0.

t = (10, 20, 30)
print(t[1])  # Output: 20

Negative Indexing

Starts from -1 (last element).

print(t[-1])   # Output: 30

4. Tuple Operations

(a) Concatenation (+)

Joins two tuples.

t1 = (1, 2)
t2 = (3, 4)
print(t1 + t2)   # (1, 2, 3, 4)

(b) Repetition (*)

Repeats the tuple.

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

(c) Membership (in / not in)

Checks if element exists.

3 in (1, 2, 3)      # True

(d) Slicing

Extracts a portion of a tuple.

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

5. Packing and Unpacking Tuple Elements

Packing

Assigning multiple values to a tuple.

t = 10, 20, 30

Unpacking

Extracting tuple values into variables.

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

6. Built-in Functions and Methods of Tuples

Function/MethodDescriptionExample
len()returns length of tuplelen(t)
tuple()converts other data types to tupletuple([1,2])
tuple(“Hello”)
count(x)counts occurrences of xt.count(10)
index(x)returns index of xt.index(20)
sorted()returns a sorted listsorted(t)
min()returns smallest elementmin(t)
max()returns largest elementmax(t)
sum()returns sum of numeric elementssum(t)

7. Tuple Assignment

Tuple assignment allows swapping without a temporary variable.

a, b = 5, 10
a, b = b, a
print(a, b)   # 10 5

8. Nested Tuples

A tuple containing other tuples.

t = (1, (2, 3), (4, 5, 6))
print(t[1][0])    # Output: 2

Suggested Programs for Practice

1. Program to find the minimum, maximum, and mean of values stored in a tuple

t = (10, 20, 30, 40, 50)

minimum = min(t)
maximum = max(t)
mean = sum(t) / len(t)

print("Minimum =", minimum)
print("Maximum =", maximum)
print("Mean =", mean)

2. Linear Search on a Tuple of Numbers

t = (5, 12, 7, 20, 15)
key = int(input("Enter number to search: "))

found = False

for i in range(len(t)):
    if t[i] == key:
        print("Found at index", i)
        found = True
        break

if not found:
    print("Not found in tuple")

3. Counting Frequency of Elements in a Tuple

Method 1: Using count()

t = (1, 2, 2, 3, 1, 4, 2)
x = 2
print("Frequency:", t.count(x))

Method 2: Using loop

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

freq = {}

for item in t:
    if item in freq:
        freq[item] += 1
    else:
        freq[item] = 1

print("Frequencies:", freq)

Short Notes / Quick Revision

  • Tuples are immutable.
  • Use ( ) brackets for tuples.
  • Single-element tuple must end with comma.
  • Supports indexing, slicing, repetition, concatenation.
  • Built-in functions: len, tuple, count, index, sorted, min, max, sum.
  • Can perform packing/unpacking.
  • Supports nested tuples.

MCQs (Multiple-Choice Questions)

1. Which of the following is a valid tuple?

a) [10, 20, 30]
b) {10, 20, 30}
c) (10, 20, 30)
d) 10, 20, 30,

Answer: c)


2. What will be the output of the following code?

t = (10,)
print(type(t))

a) <class 'int'>
b) <class 'tuple'>
c) <class 'list'>
d) Error

Answer: b)


3. Tuples in Python are:

a) Mutable
b) Immutable
c) Both
d) None

Answer: b)


4. What is the result of the following?

t = (1, 2, 3)
print(t * 2)

a) (1, 2, 3, 1, 2, 3)
b) (2, 4, 6)
c) [1, 2, 3, 1, 2, 3]
d) Error

Answer: a)


5. What does this return?

t = (10, 20, 30, 40)
print(t[1:3])

a) (10, 20)
b) (20, 30)
c) (20, 30, 40)
d) (30, 20)

Answer: b)


6. Which of the following functions returns a list and not a tuple?

a) len()
b) sorted()
c) tuple()
d) max()

Answer: b)


7. What is the output?

t = (1, 2, 3, 2, 2)
print(t.count(2))

a) 1
b) 2
c) 3
d) 4

Answer: c)


8. Which operation is NOT allowed on a tuple?

a) Indexing
b) Slicing
c) Modification of elements
d) Membership test

Answer: c)


9. What will be printed?

a, b = (10, 20)
print(a + b)

a) 30
b) (10, 20)
c) Error
d) 1020

Answer: a)


10. Which statement correctly creates a nested tuple?

a) t = (1; 2; (3,4))
b) t = [1, (2,3)]
c) t = (1, (2, 3), 4)
d) t = {1, (2,3), 4}

Answer: c)


Programming Questions

1. Write a program to find the maximum and minimum element of a tuple.

2. Write a program to find the mean of values in a tuple.

3. Write a program to search for an element in a tuple (linear search).

4. Write a program to count how many times each element appears in a tuple.

5. Write a program to reverse a tuple using slicing.

6. Write a program to extract even numbers from a tuple.

Difference Between Lists and Tuples

1. Mutability

  • List: Mutable → elements can be changed, added, or removed.
  • Tuple: Immutable → elements cannot be changed after creation.

2. Syntax

  • List: Created using square brackets [ ]
    Example: L = [1, 2, 3]
  • Tuple: Created using parentheses ( )
    Example: T = (1, 2, 3)

3. Performance and Memory

  • List: Slower and uses more memory due to mutability.
  • Tuple: Faster and uses less memory.

4. Operations

  • List: Supports many modifying methods (append, insert, remove, pop).
  • Tuple: Limited methods (count, index); no modification allowed.

5. Use Cases

  • List: Best for data that changes (e.g., marks, items in a cart).
  • Tuple: Best for fixed data (e.g., coordinates, days of week).

Quick Example

# List (mutable)
L = [10, 20, 30]
L[1] = 50  # allowed

# Tuple (immutable)
T = (10, 20, 30)
# T[1] = 50  # not allowed – will give error

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top