0% found this document useful (0 votes)
3 views4 pages

Day 23 Python Answers - 60062370 - 2025 - 05 - 17 - 21 - 28

The document covers the topic of tuples in Python, including their definition, operations, and built-in functions. It includes a question of the day about tuple assignment and immutability, as well as programming exercises for finding indices, minimum, maximum, mean, and converting between lists and tuples. The document provides examples and explanations for each concept and task.

Uploaded by

ahlawat.amit7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Day 23 Python Answers - 60062370 - 2025 - 05 - 17 - 21 - 28

The document covers the topic of tuples in Python, including their definition, operations, and built-in functions. It includes a question of the day about tuple assignment and immutability, as well as programming exercises for finding indices, minimum, maximum, mean, and converting between lists and tuples. The document provides examples and explanations for each concept and task.

Uploaded by

ahlawat.amit7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

HPSC PGT(CS)- Python Subjective Series

Day 23
Syllabus Coverage

- Tuples in Python

- Introduction to tuples, indexing, and operations: concatenation, repetition, membership,


slicing.

- Built-in functions: `len()`, `tuple()`, `count()`, `index()`, `sorted()`, `min()`, `max()`, `sum()`.

- Tuple assignment, nested tuples.

- Suggested programs: finding minimum, maximum, mean; linear search; counting frequency
of elements.

Question of the day:


1. Explain tuple assignment and nested tuples with examples. 5 M
2. Why are tuples considered immutable? How does this affect operations
like insertion and deletion? 5M

Program of the day:


1. Write a Python program to find the index of a specified element in a
tuple using the index() method. 3M
2. Given a tuple of numbers, write a program to find the minimum,
maximum, and mean (average) of the elements. 3M
3. Write a program to convert a list into a tuple and vice versa. 4M
1. Explain tuple assignment and nested tuples with examples. (5 Marks)

Answer:
Tuple Assignment:

• Allows assigning multiple variables in a single line using tuples.

• Useful for swapping values without a temporary variable.

Example:

# Tuple assignment

a, b = 10, 20

print("a =", a, "b =", b) # Output: a = 10 b = 20

# Swapping values

a, b = b, a

print("After swap: a =", a, "b =", b) # Output: After swap: a = 20 b = 10

Nested Tuples:

• A tuple inside another tuple.

• Accessed using multiple indices.

Example:

nested_tuple = (1, (2, 3), (4, (5, 6)))

print("First element:", nested_tuple[0]) # Output: First element: 1

print("Second element's first item:", nested_tuple[1][0]) # Output: 2

print("Third element's nested item:", nested_tuple[2][1][1]) # Output: 6

2. Why are tuples considered immutable? How does this affect operations like insertion and
deletion? (5 Marks)

Answer:
Immutability of Tuples:

• Tuples cannot be modified after creation (no adding, removing, or changing


elements).

• Makes them faster than lists and suitable for fixed data (e.g., dictionary keys).

Impact on Operations:

• Insertion: Not allowed. Use + to concatenate new elements into a new tuple.

tuple1 = (1, 2)

tuple2 = tuple1 + (3,) # Correct: Creates a new tuple (1, 2, 3)


• Deletion: Only possible by creating a new tuple excluding the unwanted elements.

tuple3 = (1, 2, 3, 4)

new_tuple = tuple3[:2] + tuple3[3:] # Deletes element at index 2 → (1, 2, 4)

• Modification: Direct changes (e.g., tuple[0] = 10) raise TypeError.

Programming Solutions

1. Find the index of a specified element in a tuple using the index() method. (3 Marks)

my_tuple = (10, 20, 30, 20, 40)

element = 20

# Find the first occurrence of the element

index = my_tuple.index(element)

print("Index of", element, "is:", index)

Output:

Index of 20 is: 1

2. Find the minimum, maximum, and mean of a tuple of numbers. (3 Marks)

numbers = (5, 10, 15, 20, 25)

minimum = min(numbers)

maximum = max(numbers)

mean = sum(numbers) / len(numbers)

print("Minimum:", minimum)

print("Maximum:", maximum)

print("Mean:", mean)

Output:

Minimum: 5

Maximum: 25

Mean: 15.0
3. Convert a list into a tuple and vice versa. (4 Marks)

# List to Tuple

my_list = [1, 2, 3]

list_to_tuple = tuple(my_list)

print("List converted to tuple:", list_to_tuple)

# Tuple to List

my_tuple = (4, 5, 6)

tuple_to_list = list(my_tuple)

print("Tuple converted to list:", tuple_to_list)

Output:

List converted to tuple: (1, 2, 3)

Tuple converted to list: [4, 5, 6]

You might also like