Skip to main content
Chapter 10 of 11
NCERT Solutions

Tuples and Dictionaries — NCERT Solutions

CBSE · Class 11 · Computer Science

NCERT Solutions for Tuples and Dictionaries, CBSE Class 11 Computer Science: 14 textbook questions solved step by step.

42 questions68 flashcards5 concepts

Interactive on Super Tutor

Studying Tuples and Dictionaries? Get the full interactive chapter.

Quizzes, flashcards, AI doubt-solver and a step-by-step study plan — built for NCERT solutions and more.

Free trial, no card needed.

14 Questions Solved · 3 Sections

The first 7 solutions are open to read. The other 7 are free with a Super Tutor account.

Exercise

1Consider the following tuples, tuple1 and tuple2:
tuple1 = (23,1,45,67,45,9,55,45)
tuple2 = (100,200)
Find the output of the following statements:
i. print(tuple1.index(45))
ii. print(tuple1.count(45))
iii. print(tuple1 + tuple2)
iv. print(len(tuple2))
v. print(max(tuple1))
vi. print(min(tuple1))
vii. print(sum(tuple2))
viii. print(sorted(tuple1)) then print(tuple1)
Show solution

Given:
tuple1=(23,1,45,67,45,9,55,45)\text{tuple1} = (23, 1, 45, 67, 45, 9, 55, 45)
tuple2=(100,200)\text{tuple2} = (100, 200)

i. print(tuple1.index(45))

The index() method returns the index of the first occurrence of the given element.

In tuple1, 45 first appears at index 2 (0-based indexing).

Output: 2\boxed{\text{Output: } 2}

ii. print(tuple1.count(45))

The count() method returns the number of times the element appears in the tuple.

45 appears at indices 2, 4, and 7 — a total of 3 times.

Output: 3\boxed{\text{Output: } 3}

iii. print(tuple1 + tuple2)

The + operator concatenates two tuples.

tuple1+tuple2=(23,1,45,67,45,9,55,45,100,200)\text{tuple1} + \text{tuple2} = (23, 1, 45, 67, 45, 9, 55, 45, 100, 200)

Output: (23,1,45,67,45,9,55,45,100,200)\boxed{\text{Output: } (23, 1, 45, 67, 45, 9, 55, 45, 100, 200)}

iv. print(len(tuple2))

The len() function returns the number of elements in the tuple.

tuple2 has 2 elements: 100 and 200.

Output: 2\boxed{\text{Output: } 2}

v. print(max(tuple1))

The max() function returns the largest element.

Elements of tuple1: 23, 1, 45, 67, 45, 9, 55, 45. The maximum is 67.

Output: 67\boxed{\text{Output: } 67}

vi. print(min(tuple1))

The min() function returns the smallest element.

Elements of tuple1: 23, 1, 45, 67, 45, 9, 55, 45. The minimum is 1.

Output: 1\boxed{\text{Output: } 1}

vii. print(sum(tuple2))

The sum() function returns the sum of all elements.

100+200=300100 + 200 = 300

Output: 300\boxed{\text{Output: } 300}

viii. print(sorted(tuple1)) then print(tuple1)

The sorted() function returns a new sorted list from the tuple elements; the original tuple remains unchanged.

Sorted elements of tuple1 in ascending order: 1, 9, 23, 45, 45, 45, 55, 67.

Output of print(sorted(tuple1)):  [1, 9, 23, 45, 45, 45, 55, 67]
Output of print(tuple1):          (23, 1, 45, 67, 45, 9, 55, 45)

sorted(tuple1) gives a list [1,9,23,45,45,45,55,67]; tuple1 remains (23,1,45,67,45,9,55,45)\boxed{\text{sorted(tuple1) gives a list } [1, 9, 23, 45, 45, 45, 55, 67]; \text{ tuple1 remains } (23, 1, 45, 67, 45, 9, 55, 45)}

2Consider the following dictionary stateCapital:
stateCapital = {"AndhraPradesh": "Hyderabad", "Bihar": "Patna", "Maharashtra": "Mumbai", "Rajasthan": "Jaipur"}
Find the output of the following statements:
i. print(stateCapital.get("Bihar"))
ii. print(stateCapital.keys())
iii. print(stateCapital.values())
iv. print(stateCapital.items())
v. print(len(stateCapital))
vi. print("Maharashtra" in stateCapital)
vii. print(stateCapital.get("Assam"))
viii. del stateCapital["Rajasthan"] then print(stateCapital)
Show solution

Given:

stateCapital = {"AndhraPradesh": "Hyderabad",
                "Bihar": "Patna",
                "Maharashtra": "Mumbai",
                "Rajasthan": "Jaipur"}

i. print(stateCapital.get("Bihar"))

The get() method returns the value associated with the given key.

Key "Bihar" maps to "Patna".

Output: Patna\boxed{\text{Output: Patna}}

ii. print(stateCapital.keys())

The keys() method returns a view object containing all the keys of the dictionary.

Output: dict_keys([’AndhraPradesh’, ’Bihar’, ’Maharashtra’, ’Rajasthan’])\boxed{\text{Output: dict\_keys(['AndhraPradesh', 'Bihar', 'Maharashtra', 'Rajasthan'])}}

iii. print(stateCapital.values())

The values() method returns a view object containing all the values of the dictionary.

Output: dict_values([’Hyderabad’, ’Patna’, ’Mumbai’, ’Jaipur’])\boxed{\text{Output: dict\_values(['Hyderabad', 'Patna', 'Mumbai', 'Jaipur'])}}

iv. print(stateCapital.items())

The items() method returns a view object containing all key-value pairs as tuples.

Output: dict_items([(’AndhraPradesh’, ’Hyderabad’), (’Bihar’, ’Patna’), (’Maharashtra’, ’Mumbai’), (’Rajasthan’, ’Jaipur’)])\boxed{\text{Output: dict\_items([('AndhraPradesh', 'Hyderabad'), ('Bihar', 'Patna'), ('Maharashtra', 'Mumbai'), ('Rajasthan', 'Jaipur')])}}

v. print(len(stateCapital))

The len() function returns the number of key-value pairs in the dictionary.

There are 4 key-value pairs.

Output: 4\boxed{\text{Output: } 4}

vi. print("Maharashtra" in stateCapital)

The in operator checks whether the given key exists in the dictionary.

"Maharashtra" is indeed a key in stateCapital, so the result is True.

Output: True\boxed{\text{Output: True}}

vii. print(stateCapital.get("Assam"))

The get() method returns None if the key does not exist (instead of raising a KeyError).

"Assam" is not a key in stateCapital.

Output: None\boxed{\text{Output: None}}

viii. del stateCapital["Rajasthan"] then print(stateCapital)

The del statement removes the key-value pair with key "Rajasthan" from the dictionary.

After deletion:

Output: {’AndhraPradesh’: ’Hyderabad’, ’Bihar’: ’Patna’, ’Maharashtra’: ’Mumbai’}\boxed{\text{Output: \{'AndhraPradesh': 'Hyderabad', 'Bihar': 'Patna', 'Maharashtra': 'Mumbai'\}}}

3"Lists and Tuples are ordered". Explain.Show solution

Concept: Ordered sequences in Python.

A sequence is said to be ordered if each element in it has a fixed position (index), and the order in which elements are stored is maintained throughout the lifetime of the object.

Both Lists and Tuples are ordered because:

  1. Every element is assigned a definite index starting from 0 (positive indexing) or −1 from the end (negative indexing).
  2. The elements are stored and retrieved in the same order in which they were inserted.
  3. Two lists/tuples with the same elements but in different order are considered different.

Example:

list1  = [10, 20, 30]
list2  = [30, 20, 10]
print(list1 == list2)   # Output: False  (order matters)

tuple1 = (10, 20, 30)
tuple2 = (30, 20, 10)
print(tuple1 == tuple2) # Output: False  (order matters)

print(list1[0])   # Output: 10  (fixed position)
print(tuple1[2])  # Output: 30  (fixed position)

Because each element occupies a fixed, well-defined position, both lists and tuples are called ordered data structures. This is in contrast to dictionaries (in older Python versions) and sets, which do not guarantee element order.

4With the help of an example show how can you return more than one value from a function.Show solution

Concept: In Python, a function can return multiple values by packing them into a tuple. The caller can then unpack the tuple into individual variables.

Example Program:

def calculate(a, b):
    """Returns sum, difference, product and quotient of two numbers."""
    total      = a + b
    difference = a - b
    product    = a * b
    quotient   = a / b
    return total, difference, product, quotient   # returns a tuple

# Calling the function
s, d, p, q = calculate(20, 4)

print("Sum        =", s)   # 24
print("Difference =", d)   # 16
print("Product    =", p)   # 80
print("Quotient   =", q)   # 5.0

Output:

Sum        = 24
Difference = 16
Product    = 80
Quotient   = 5.0

Explanation:

  • The return statement packs all four values into a tuple (24, 16, 80, 5.0) automatically.
  • On the calling side, tuple unpacking assigns each value to the corresponding variable.
  • This is the standard Pythonic way to return more than one value from a function.
5What advantages do tuples have over lists?Show solution

Advantages of Tuples over Lists:

FeatureTupleList
MutabilityImmutable (cannot be changed)Mutable (can be changed)
SpeedFaster to create and accessSlightly slower
MemoryConsumes less memoryConsumes more memory
SafetyData is write-protectedData can be accidentally modified
Use as dict keyCan be used as dictionary keyCannot be used as dictionary key
HashableYesNo

Detailed Advantages:

  1. Immutability / Data Integrity: Since tuples cannot be modified after creation, they protect data from accidental changes. This makes them ideal for storing constant or read-only data (e.g., days of the week, coordinates).
  1. Faster Execution: Iterating over a tuple is faster than iterating over a list of the same size because Python can optimise immutable objects.
  1. Less Memory: Tuples occupy less memory space compared to lists.
   import sys
   print(sys.getsizeof((1,2,3)))   # smaller
   print(sys.getsizeof([1,2,3]))   # larger
  1. Can be used as Dictionary Keys: Because tuples are hashable (immutable), they can serve as keys in a dictionary, whereas lists cannot.
   location = {(28.6, 77.2): "Delhi"}   # valid
  1. Safe for Unpacking / Multiple Return Values: Functions commonly use tuples to return multiple values safely.
  1. Can be stored in sets: Tuples can be elements of a set; lists cannot.
6When to use tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.Show solution

When to use a Tuple:

Use a tuple when:

  • The data should not change after creation (immutable, read-only data).
  • You need to use the collection as a dictionary key.
  • You want faster access and less memory usage.
  • You are returning multiple values from a function.
  • The data represents a fixed record (like a row in a database).

Examples of Tuple usage:

# 1. Storing fixed data — days of the week
days = ('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')

# 2. Geographic coordinates (should not change)
delhi_coords = (28.6139, 77.2090)

# 3. Returning multiple values from a function
def min_max(lst):
    return min(lst), max(lst)   # returns a tuple

# 4. RGB colour values
red = (255, 0, 0)

When to use a Dictionary:

Use a dictionary when:

  • Data is in key-value pairs and you need fast lookup by key.
  • Keys are unique identifiers (like roll numbers, usernames, product IDs).
  • You need to map one piece of information to another.
  • The data needs to be updated frequently.

Examples of Dictionary usage:

# 1. Student records — roll number mapped to name
students = {101: 'Aman', 102: 'Priya', 103: 'Rahul'}

# 2. Phone book — name mapped to phone number
phonebook = {'Aman': '9876543210', 'Priya': '9123456789'}

# 3. Word frequency count
sentence = 'to be or not to be'
freq = {}
for word in sentence.split():
    freq[word] = freq.get(word, 0) + 1
# freq = {'to': 2, 'be': 2, 'or': 1, 'not': 1}

# 4. State and capital mapping
state_capital = {'Rajasthan': 'Jaipur', 'Bihar': 'Patna'}

Summary: Use a tuple for fixed, ordered, immutable collections; use a dictionary for fast, key-based lookup of mutable, unordered key-value data.

7Prove with the help of an example that the variable is rebuilt in case of immutable data types.Show solution

Concept: Immutable data types (int, float, string, tuple) cannot be changed in place. When we try to 'modify' a variable of an immutable type, Python creates a new object in memory and makes the variable point to it. The original object remains unchanged.

We can verify this using the built-in id() function, which returns the memory address of an object.

Example with an integer (immutable):

a = 10
print("Before modification:")
print("Value of a =", a)
print("id of a    =", id(a))

a = a + 5    # 'modifying' a
print("\nAfter modification:")
print("Value of a =", a)
print("id of a    =", id(a))

Sample Output:

Before modification:
Value of a = 10
id of a    = 140720000000000

After modification:
Value of a = 15
id of a    = 140720000000160

Observation: The id (memory address) of a changed after the assignment. This proves that a new integer object with value 15 was created and a now points to it. The old object (10) was not modified.

Example with a tuple (immutable):

t = (1, 2, 3)
print("Before:", t, "id =", id(t))

t = t + (4,)   # creates a new tuple
print("After: ", t, "id =", id(t))

Sample Output:

Before: (1, 2, 3)    id = 2345678901
After:  (1, 2, 3, 4) id = 2345679999

Conclusion: Every time we appear to 'change' an immutable variable, Python rebuilds (creates a new object) and reassigns the variable to the new object. The identity (id) changes, proving that the original object was not modified — it was replaced.

8TypeError occurs while statement 2 is running. Give reason. How can it be corrected?
>>> tuple1 = (5) #statement 1
>>> len(tuple1) #statement 2

Free with a Super Tutor account

Programming Problems

1Write a program to read email IDs of n number of students and store them in a tuple. Create two new tuples, one to store only the usernames from the email IDs and second to store domain names from the email IDs. Print all three tuples at the end of the program. [Hint: You may use the function split()]

Free with a Super Tutor account

2Write a program to input names of n students and store them in a tuple. Also, input a name from the user and find if this student is present in the tuple or not.

Free with a Super Tutor account

3Write a Python program to find the highest 2 values in a dictionary.

Free with a Super Tutor account

4Write a Python program to create a dictionary from a string.
Note: Track the count of the letters from the string.
Sample string: 'w3resource'
Expected output: {'3': 1, 's': 1, 'r': 2, 'u': 1, 'w': 1, 'c': 1, 'e': 2, 'o': 1}

Free with a Super Tutor account

5Write a program to input your friends' names and their Phone Numbers and store them in the dictionary as the key-value pair. Perform the following operations on the dictionary:
a) Display the name and phone number of all your friends
b) Add a new key-value pair in this dictionary and display the modified dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
f) Display the dictionary in sorted order of names

Free with a Super Tutor account

Case Study-Based Question (SMIS System)

1Write a program to take in the roll number, name and percentage of marks for n students of Class X. Write user defined functions to:
- accept details of the n students
- search details of a particular student on the basis of roll number and display result
- display the result of all the students
- find the topper amongst them
- find the subject toppers amongst them
(Hint: use Dictionary, where the key can be roll number and the value is an immutable data type containing name and percentage)

Free with a Super Tutor account

7 more solved questions in Tuples and Dictionaries

They are free with a Super Tutor account, along with practice quizzes and flashcards for this chapter. Free to start, no card needed.

Frequently Asked Questions

What are the important topics in Tuples and Dictionaries for CBSE Class 11 Computer Science?
Key topics in Tuples and Dictionaries include Tuples: Basic Idea and Creation, Tuple Properties and Operations, Tuple Methods and Built-in Functions, Tuple Assignment and Nested Tuples. Study these first, then practise questions on each for Class 11 exams.
Are these NCERT Solutions for Tuples and Dictionaries free?
The first 7 of the 14 solutions on this page are open to read. The other 7 are free with a Super Tutor account — signing up is free and needs no card.
How should I revise Tuples and Dictionaries for Class 11 exams?
Learn the core ideas first, then work through the 42 practice questions on Tuples and Dictionaries. Revise definitions regularly and use flashcards for quick recall before the exam.

Sources & Official References

Content is aligned to the official syllabus. Refer to the board website for the latest curriculum.

For serious students

Get the full Tuples and Dictionaries chapter — start free.

Quizzes, flashcards, an AI doubt solver and a study plan for CBSE Class 11 Computer Science. Free to start, no card needed.