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.
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.
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:
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).
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.
iii. print(tuple1 + tuple2)
The + operator concatenates two tuples.
iv. print(len(tuple2))
The len() function returns the number of elements in the tuple.
tuple2 has 2 elements: 100 and 200.
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.
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.
vii. print(sum(tuple2))
The sum() function returns the sum of all elements.
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)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".
ii. print(stateCapital.keys())
The keys() method returns a view object containing all the keys of the dictionary.
iii. print(stateCapital.values())
The values() method returns a view object containing all the values of the dictionary.
iv. print(stateCapital.items())
The items() method returns a view object containing all key-value pairs as tuples.
v. print(len(stateCapital))
The len() function returns the number of key-value pairs in the dictionary.
There are 4 key-value pairs.
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.
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.
viii. del stateCapital["Rajasthan"] then print(stateCapital)
The del statement removes the key-value pair with key "Rajasthan" from the dictionary.
After deletion:
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:
- Every element is assigned a definite index starting from 0 (positive indexing) or −1 from the end (negative indexing).
- The elements are stored and retrieved in the same order in which they were inserted.
- 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.0Output:
Sum = 24
Difference = 16
Product = 80
Quotient = 5.0Explanation:
- The
returnstatement 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:
| Feature | Tuple | List |
|---|---|---|
| Mutability | Immutable (cannot be changed) | Mutable (can be changed) |
| Speed | Faster to create and access | Slightly slower |
| Memory | Consumes less memory | Consumes more memory |
| Safety | Data is write-protected | Data can be accidentally modified |
| Use as dict key | Can be used as dictionary key | Cannot be used as dictionary key |
| Hashable | Yes | No |
Detailed Advantages:
- 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).
- Faster Execution: Iterating over a tuple is faster than iterating over a list of the same size because Python can optimise immutable objects.
- 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- 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- Safe for Unpacking / Multiple Return Values: Functions commonly use tuples to return multiple values safely.
- 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 = 140720000000160Observation: 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 = 2345679999Conclusion: 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.
>>> tuple1 = (5) #statement 1
>>> len(tuple1) #statement 2
Free with a Super Tutor account
Programming Problems
Free with a Super Tutor account
Free with a Super Tutor account
Free with a Super Tutor account
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
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)
- 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?
Are these NCERT Solutions for Tuples and Dictionaries free?
How should I revise Tuples and Dictionaries for Class 11 exams?
Sources & Official References
- NCERT Official — ncert.nic.in
- CBSE Academic — cbseacademic.nic.in
- CBSE Official — cbse.gov.in
- National Education Policy 2020 — education.gov.in
Content is aligned to the official syllabus. Refer to the board website for the latest curriculum.
More resources for Tuples and Dictionaries
Practice Quiz
Test yourself with a quick quiz
Important Questions
Exam-style questions with answers
Revision Notes
Key points for last-minute revision
Chapter Summary
Understand the chapter at a glance
Concept Maps
See how topics connect
Study Plan
Step-by-step plan for this chapter
Flashcards
Quick-fire cards for active recall
Syllabus
What topics to cover
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.