📌 Snapshot
- Two important Python data structures appear here: tuples (immutable ordered sequences) and dictionaries (mutable key-value mappings), both essential for efficient data storage and retrieval.
- Tuples are contrasted with lists: tuples are immutable (cannot be modified after creation), making them safer for fixed data; lists are mutable. Iterating through a tuple is faster than through a list.
- Dictionaries store data as key-value pairs enclosed in curly braces; keys must be unique and immutable (number, string, or tuple), while values can be of any type and can repeat.
- CUET tests these structures frequently — Python-specific data types, operations, built-in methods, and the distinction between mutable and immutable structures are all common MCQ territory.
- Practical programs (swapping variables, returning multiple values from functions, character frequency count) demonstrate real-world use of these structures and are a source of case/code-based questions.
📖 Detailed Notes
2.1 Core concepts
- Tuple definition: A tuple is an ordered sequence of elements of different data types (integer, float, string, list, or even another tuple). Elements are enclosed in parentheses and separated by commas. Indexing starts from 0, and negative indexing is supported.
(NCERT §10.1, p. 207) - Single-element tuple: If a tuple has only one element, that element must be followed by a comma — e.g.,
(20,). Without the trailing comma, Python treats it as the data type of the element, not as a tuple. A sequence of comma-separated values without parentheses is also treated as a tuple by default.(NCERT §10.1, p. 207–208) - Accessing elements: Elements of a tuple are accessed using indexing (
tuple1[0]) and slicing (tuple1[2:7]), exactly like strings and lists. Accessing an index out of range raisesIndexError.(NCERT §10.1.1, p. 208) - Tuple immutability: Tuples are immutable — once created, individual elements cannot be changed. Attempting
tuple1[4] = 10raisesTypeError: 'tuple' object does not support item assignment. However, if an element is itself a mutable type (e.g., a list), that element's contents can be modified.(NCERT §10.1.2, p. 208–209) - Concatenation (
+): Two tuples can be joined using the+operator to produce a new tuple. Extending an existing tuple via concatenation creates a new tuple rather than modifying the original.(NCERT §10.2.1, p. 209) - Repetition (
*): The*operator repeats the elements of a tuple a specified number of times. The first operand must be a tuple and the second must be an integer.(NCERT §10.2.2, p. 210) - Membership (
in,not in): Theinoperator returnsTrueif an element is present in the tuple;not inreturnsTrueif it is absent.(NCERT §10.2.3, p. 210) - Slicing: Slicing extracts a sub-tuple using
tuple[start:stop:step]. Negative step reverses the tuple (e.g.,tuple1[::-1]).(NCERT §10.2.4, p. 210–211) - Tuple methods and built-in functions: Table 10.1 lists:
len(),tuple(),count(),index(),sorted(),min(),max(),sum().count()returns the frequency of a given element;index()returns the position of its first occurrence (raisesValueErrorif not found);sorted()returns a new sorted list without changing the original tuple.(NCERT §10.3, p. 211–212) - Tuple assignment (unpacking): Python allows a tuple of variables on the left side of
=to be assigned values from a tuple on the right. The number of variables must equal the number of elements, otherwise aValueErroris raised. Expressions on the right side are evaluated first.(NCERT §10.4, p. 212–213) - Nested tuples: A tuple inside another tuple is called a nested tuple. Useful for storing structured records, e.g., student roll number, name, and marks.
(NCERT §10.5, p. 213) - Tuple handling programs: Tuples enable elegant operations such as swapping two variables without a temporary variable —
(num1, num2) = (num2, num1)— and functions returning multiple values as a tuple.(NCERT §10.6, p. 213–214) - Introduction to dictionaries: A dictionary is a mapping data type — a mapping between a set of keys and a set of values. Each key-value pair is called an item; key and value are separated by a colon (
:); items are separated by commas and enclosed in curly braces. Items in a dictionary are ordered (Python 3.7+).(NCERT §10.7, p. 215) - Creating a dictionary: Keys must be unique and immutable (number, string, or tuple). Values can be of any type and can repeat. An empty dictionary is created with
{}ordict().(NCERT §10.7.1, p. 215–216) - Accessing dictionary items: Items are accessed by key (not by position).
dict1['key']returns the corresponding value; accessing a non-existent key raisesKeyError.(NCERT §10.7.2, p. 216) - Dictionaries are mutable: New items can be added by assigning to a new key (
dict1['Meena'] = 78). Existing items are modified by reassigning to an existing key.(NCERT §10.8, p. 216–217) - Dictionary operations: The
inoperator checks if a key is present (True/False);not indoes the opposite. Dictionary traversal using aforloop iterates over keys (Method 1) or key-value pairs using.items()(Method 2).(NCERT §10.9–10.10, p. 217–218) - Dictionary methods: Table 10.2 lists:
len(),dict(),keys(),values(),items(),get(),update(),del,clear().get()returns the value for a key orNoneif the key is absent (noKeyError).update()merges another dictionary into the current one.del dict1['key']deletes a specific item;del dict1deletes the entire dictionary.clear()removes all items but the dictionary object remains.(NCERT §10.11, p. 218–219) - Manipulating dictionaries (programs): Programs demonstrate: building a frequency-count dictionary of characters in a string (Program 10-7), converting digits to word-names using a dictionary (Python's idiomatic lookup-table pattern), and creating/operating on an odd-numbers dictionary (Program 10-5).
(NCERT §10.12, p. 219–222) - Tuples are hashable (NCERT §10.7.1 cross-link). Because tuples are immutable, they can act as dictionary keys or as members of a set. A tuple of all-immutable elements is fully hashable; a tuple containing a list is not hashable.
- Iteration over a dictionary defaults to keys (NCERT §10.10, p. 218).
for k in d:yields each key. To iterate over values, usefor v in d.values(); for pairs, usefor k, v in d.items(). Theitems()view always returns (key, value) tuples in insertion order in Python 3.7+. - Order preservation (NCERT note, p. 215). From Python 3.7 onwards, dictionaries preserve insertion order. NCERT mentions this — earlier versions did not guarantee order. NTA distractors based on outdated information may claim dictionaries are "unordered" in modern Python — this is misleading.
- Heterogeneous values (NCERT §10.7.1, p. 215). A single dictionary may map keys to values of different types — e.g.,
{'name': 'Akansha', 'marks': [98, 95, 99]}mixes string and list values. This makes dictionaries versatile for structured record storage. - Common idiom: "Frequency counting". NCERT Program 10-7 uses the pattern
d[ch] = d.get(ch, 0) + 1— a Pythonic way to safely increment a possibly-missing key. Withoutget()an extraif ch in d:check would be needed. - The
len()builtin works on tuples and dictionaries (NCERT §10.3, §10.11). It counts elements in tuples and key-value pairs in dictionaries. Same builtin applies uniformly across sequence and mapping types. - Dictionaries can be nested (NCERT §10.7.1). A value in a dictionary can itself be another dictionary —
{'alice': {'age': 16, 'class': 'XI'}, 'bob': {'age': 17, 'class': 'XII'}}. Access uses chained brackets:d['alice']['age'].
2.2 Definitions to memorise
| Term | Definition | Page |
|---|---|---|
| Tuple | An ordered, immutable sequence of elements of different data types, enclosed in parentheses separated by commas. | 207 |
| Immutable | A data type whose elements cannot be changed after creation. | 208 |
| Nested tuple | A tuple that contains another tuple as one of its elements. | 213 |
| Tuple assignment (unpacking) | Assigning values from a right-hand tuple to a matching number of variables on the left-hand side of the assignment operator. | 212 |
| Dictionary | A mapping (non-scalar) data type consisting of key-value pairs enclosed in curly braces; keys are unique and immutable. | 215 |
| Item (dictionary) | A key-value pair in a dictionary, with key and value separated by a colon. | 215 |
| KeyError | Error raised when a key that does not exist is accessed in a dictionary. | 216 |
| Mutable | A data type whose elements can be changed after creation (e.g., list, dictionary). | 216 |
| get() | Dictionary method that returns the value for a given key, or None if the key is absent, without raising KeyError. |
219 |
| update() | Dictionary method that appends key-value pairs from a second dictionary into the first. | 219 |
| clear() | Dictionary method that deletes all items from the dictionary, leaving an empty dictionary object. | 219 |
keys() |
Dictionary method returning a view of all keys | 218 |
values() |
Dictionary method returning a view of all values | 218 |
items() |
Dictionary method returning a view of all (key, value) tuple pairs | 218 |
del keyword |
Deletes a specific key (del d['k']) or the whole dictionary (del d) |
219 |
count() (tuple) |
Returns the number of times an element appears in the tuple | 211 |
index() (tuple) |
Returns the first index of an element; raises ValueError if absent | 211 |
min(), max(), sum() |
Built-ins returning smallest, largest, and sum of tuple elements | 212 |
| Mapping data type | Data type where each value is accessed via an immutable key (Python's dict is the standard example) | 215 |
| Single-element tuple | Tuple containing one element written with a trailing comma e.g., (20,) |
207 |
| Swap via tuple | Pythonic idiom (a, b) = (b, a) to exchange two variables without a temporary |
213 |
2.3 Diagrams / processes to remember
- Table 10.1 — Built-in functions and methods for tuples (p. 211–212): A reference table covering
len(),tuple(),count(),index(),sorted(),min(),max(),sum()with syntax and examples. Students should be able to predict output for each function given a sample tuple. - Table 10.2 — Built-in functions and methods for dictionary (p. 218–219): A reference table covering
len(),dict(),keys(),values(),items(),get(),update(),del,clear(). Particularly focus on the difference betweendel dict1['key'](removes one item),del dict1(removes the entire object), andclear()(empties the dictionary but object persists). - Program 10-1 — Nested tuple for student records (p. 213): Shows how
st[i][0],st[i][1],st[i][2]accesses roll number, name, and marks respectively from a nested tuple; useful model for 2D data access questions. - Program 10-3 — Function returning a tuple (p. 214): A function
circle(r)returns(area, circumference)as a tuple — demonstrates how Python functions can return multiple values. - Slicing summary (p. 210–211):
tuple[start:stop],tuple[:stop],tuple[start:],tuple[::step],tuple[::-1](reverse). Understand that slicing on a tuple returns a tuple.
2.4 Common confusions / NTA trap points
- Trailing comma for single-element tuple:
(20)is an integer;(20,)is a tuple. NTA may givetype((20))andtype((20,))as options — only the second is<class 'tuple'>. sorted()returns a list, not a tuple:sorted(tuple1)returns a new list in ascending order and does not modify the original tuple. A common distractor says it returns a sorted tuple or modifies the tuple in place.inoperator on dictionaries checks keys, not values:'Suhel' in dict1isTrueif'Suhel'is a key. NTA may frame questions where students assume it checks values.index()vscount()confusion:index()returns the position of the first occurrence;count()returns the number of occurrences. Both raise errors/return 0 differently when the element is absent (ValueErrorforindex(),0forcount()).- Mutable element inside an immutable tuple: Even though a tuple is immutable, if it contains a list, that list's elements can be changed.
tuple2[3][1] = 10is valid iftuple2[3]is a list. NTA may test whether this raises an error. del dict1vsdict1.clear()(NCERT Table 10.2, p. 219). Afterdel dict1, the namedict1no longer exists (NameErrorif referenced again). Afterdict1.clear(),dict1still exists as an empty dictionary{}.- Dictionary keys must be immutable (NCERT §10.7.1, p. 215). Lists cannot be used as keys (they are mutable). Tuples and strings can. NTA distractor: claims any object can be a dictionary key.
get()returns None for missing keys (NCERT Table 10.2, p. 219). Unlikedict1['x']which raises KeyError,dict1.get('x')silently returns None. NTA distractor swaps these behaviours.- Tuples allow nested mutable elements (NCERT §10.1.2, p. 209). While the tuple's structure is immutable, mutable elements inside can be modified. NTA tests this nuance.
- Tuple unpacking requires exact match (NCERT §10.4, p. 213).
a, b = (1, 2, 3)raises ValueError. NTA distractor: claims extras are silently ignored. - Concatenation creates a new tuple (NCERT §10.2.1, p. 209).
t1 + t2does not modifyt1ort2— the result is a fresh tuple.
🎯 Practice MCQs
First 3 questions free · create a free account to unlock the rest — answers & explanations included, no payment needed
Q1. What will be the output of the following Python code? ```python >>> tuple5 = (20) >>> type(tuple5) ```
▸ Show answer & explanation
Answer: C
`(20)` without a trailing comma is treated as an integer expression in parentheses, so `type(tuple5)` returns `<class 'int'>`. To make it a tuple, the correct syntax is `(20,)`. ---
Q2. Consider the following tuple: `tuple1 = (10, 20, 30, 10, 40, 10, 50)`. What is the output of `tuple1.count(10)`?
▸ Show answer & explanation
Answer: C
The value 10 appears at index positions 0, 3, and 5 in `tuple1`, so `count(10)` returns 3. `count()` counts all occurrences, not just the first. ---
Q3. Which of the following statements about tuples and dictionaries in Python is CORRECT?
▸ Show answer & explanation
Answer: C
Option C is exactly correct. Options A and B reverse the facts. Option D is incorrect because values can repeat and can be of any data type including mutable types. ---
🔒 12 more practice MCQs
Create a free account to unlock every MCQ in this chapter — answers and explanations included. No payment needed.
Already registered? Just log in and they'll all appear here.
Q4. Assertion (A): After executing `dict1.clear()`, the statement `print(dict1)` will raise a `NameError`. Reason (R): `clear()` removes all items from a dictionary but the dictionary object itself still exists in memory.
▸ Show answer & explanation
Answer: C
The Assertion is false: `print(dict1)` after `clear()` prints `{}`, not a `NameError` — `NameError` would only occur after `del dict1`. The Reason is true: `clear()` empties the dictionary but the object persists. ---
Q5. What will be the output of the following code? ```python tuple1 = (1, 3, 5, 7, 9) tuple2 = (2, 4, 6, 8, 10) tuple3 = tuple1 + tuple2 print(tuple3[3]) ```
▸ Show answer & explanation
Answer: A
`tuple3 = (1, 3, 5, 7, 9, 2, 4, 6, 8, 10)`. Index 3 (zero-based) is the 4th element, which is `7`. A common distractor is `8` (index 3 of `tuple2`), but concatenation places `tuple1` elements first. ---
Q6. Match the following dictionary methods with their correct descriptions: | Method | Description | |---|---| | P. `keys()` | 1. Returns value for a key; returns None if key is absent | | Q. `items()` | 2. Returns a list of all keys in the dictionary | | R. `get()` | 3. Merges key-value pairs of one dictionary into another | | S. `update()` | 4. Returns a list of tuples (key, value) pairs |
▸ Show answer & explanation
Answer: A
`keys()` returns all keys (→2); `items()` returns list of (key, value) tuples (→4); `get()` returns value or `None` without `KeyError` (→1); `update()` merges another dictionary (→3). Option A is the only mapping that is fully correct. ---
Q7. Output of `len({'a':1, 'b':2, 'c':3})`?
▸ Show answer & explanation
Answer: B
`len()` on a dictionary returns the number of key-value pairs. ---
Q8. Output: ```python d = {'A': 1, 'B': 2} d['C'] = 3 print(len(d)) ```
▸ Show answer & explanation
Answer: B
Adding `d['C'] = 3` inserts a new item. New length = 3. ---
Q9. Output: ```python t = (1, 2, 3) t[0] = 99 print(t) ```
▸ Show answer & explanation
Answer: C
Item assignment on a tuple raises `TypeError`. ---
Q10. Output of `d.get('z', 0)` where `d = {'a':1}`?
▸ Show answer & explanation
Answer: B
`get(key, default)` returns the default value if the key is absent. ---
Q11. Which is a valid dictionary key?
▸ Show answer & explanation
Answer: C
Keys must be immutable. Tuples qualify; lists, sets, and dicts do not. ---
Q12. Output: ```python a, b = 5, 10 a, b = b, a print(a, b) ```
▸ Show answer & explanation
Answer: B
Tuple unpacking swaps `a` and `b` in one step. ---
Q13. Output of `('Hi',) * 3`?
▸ Show answer & explanation
Answer: B
`*` on a tuple repeats elements. ---
Q14. What does the following print? ```python d = {'a': 1, 'b': 2} for k, v in d.items(): print(k, v, end=' | ') ```
▸ Show answer & explanation
Answer: A
`items()` yields (key, value) tuples; unpacked into k, v. ---
Q15. Assertion (A): A dictionary key may be `(1, 2)` but not `[1, 2]`. Reason (R): Dictionary keys must be immutable; tuples are immutable but lists are mutable.
▸ Show answer & explanation
Answer: A
Immutability of keys is the rule. Tuples satisfy it; lists do not.
📊 Previous-Year Questions
Practise with real CUET Computer Science previous-year papers — every question solved, with the correct answer and a step-by-step explanation.
View solved CUET PYQ papers →Ready to drill Computer Science?
Unlock all MCQs, chapter tests, mocks & PYQs for ₹199/year.
Get UniDrill Pro