CBSETest.comby Bimal Publications

Need help with Tuples and Dictionaries?

Practice Tests
Class 11 · Computer Science NCERT Class 11 Computer Science · Ch. 104 min read · 15 questions

Tuples and Dictionaries

Computer Science

Tuples and Dictionaries

Tuples and Dictionaries in Python

Python offers two more essential built-in collections: tuples and dictionaries. Tuples are immutable ordered sequences; dictionaries are mutable mappings of key-value pairs.

---

Part A — Tuples

A tuple is an ordered, immutable collection enclosed in parentheses ( ). Once created, its elements cannot be changed, making it ideal for fixed data like coordinates or RGB values.

Creating Tuples

```
empty = ()
single = (42,) # trailing comma is mandatory for single-element tuple
coords = (10.5, 20.3)
mixed = (1, "hello", True)
```

Example 1

Indexing and slicing (same rules as lists)
```
t = (10, 20, 30, 40)
print(t[1]) # 20
print(t[-1]) # 40
print(t[1:3]) # (20, 30)
```

Example 2

Tuple operations
```
a = (1, 2)
b = (3, 4)
print(a + b) # (1, 2, 3, 4) — concatenation
print(a · 3) # (1, 2, 1, 2, 1, 2) — repetition
print(3 in (1,2,3,4)) # True
```

Example 3

Tuple unpacking
```
point = (5, 10)
x, y = point
print(x, y) # 5 10
```

Tuple methods: count() and index() — same behaviour as in lists.

---

Part B — Dictionaries

A dictionary is an unordered (Python 3.7+ maintains insertion order) collection of key:value pairs enclosed in curly braces { }. Keys must be unique and immutable (strings, numbers, tuples); values can be any type.

Creating a Dictionary

```
emptyd = {}
student = {"name": "Ananya", "class": 11, "marks": 92}
```

Example 4

Accessing and modifying values
```
d = {"fruit": "mango", "qty": 10}
print(d["fruit"]) # mango
d["qty"] = 15 # update existing key
d["price"] = 50 # add new key
print(d)
```

Example 5

Dictionary methods
```
d = {"a": 1, "b": 2, "c": 3}
print(d.keys()) # dictkeys(['a', 'b', 'c'])
print(d.values()) # dictvalues([1, 2, 3])
print(d.items()) # dictitems([('a',1),('b',2),('c',3)])
print(d.get("x", 0)) # 0 (key absent, default returned)
d.update({"d": 4}) # add or update entries
del d["b"] # delete key 'b'
```

Example 6

Iterating over a dictionary
```
inventory = {"pen": 5, "book": 12, "pencil": 8}
for item, count in inventory.items():
print(item, "→", count)
```

Example 7

Nested dictionary
```
school = {
"class11": {"students": 40, "teacher": "Mr. Rao"},
"class12": {"students": 38, "teacher": "Ms. Jain"}
}
print(school["class11"]["teacher"]) # Mr. Rao
```

---

Common mistakes

  • Single-element tuple — (5) is an int; (5,) is a tuple. The trailing comma is essential.
  • Using a mutable key in a dict — using a list as a dictionary key raises TypeError; use a tuple instead.
  • KeyError — accessing a missing key directly raises KeyError; use get() or check with in operator.
  • Tuple immutability — you cannot change t[0] = 99; create a new tuple instead.

---

Summary

Tuples are immutable ordered sequences — useful for fixed data, function return values, and as dictionary keys. Dictionaries store key-value pairs, offer O(1) average-case lookup, and provide methods like keys(), values(), items(), get(), and update() for flexible data management.

Practice Problems

15 questions with instant feedback.

Question 1 of 15Score 0

Which of the following correctly creates a tuple with a single element?