REVISION SUMMARY: Tuples and Dictionaries (NCERT Class 11)
1. Chapter at a glance
- A tuple is an ordered sequence of elements of different data types enclosed in parentheses and separated by commas; a sequence without parentheses is treated as a tuple by default.
- Tuple is immutable, so its elements cannot be changed after creation (though a mutable element like a list inside it can be modified).
- Tuples support concatenation (+), repetition (*), membership (in / not in) and slicing operations similar to strings and lists.
- Tuple assignment allows unpacking values from a tuple on the right side into variables on the left (number of variables must match).
- A nested tuple is a tuple inside another tuple; it can store structured records (e.g., student details).
- Dictionary is a mutable mapping data type consisting of key-value pairs (items) enclosed in curly braces; keys are unique and immutable, values can be repeated.
- Dictionary items are accessed using keys (not indices); membership, traversal and manipulation are performed on keys.
- Built-in functions and methods for tuples include len(), tuple(), count(), index(), sorted(), min(), max(), sum(); for dictionaries include len(), dict(), keys(), values(), items(), get(), update(), del(), clear().
2. Key terms and definitions
- Tuple: An ordered sequence of elements of different data types enclosed in parentheses (round brackets) and separated by commas.
- Immutable (tuple): Elements of a tuple cannot be changed after the tuple is created.
- Nested tuple: A tuple inside another tuple.
- Tuple assignment: Assigning values from a tuple on the right side of = to a tuple of variables on the left.
- Dictionary: A mapping between a set of keys and a set of values; each key-value pair is called an item.
- Item (in dictionary): A key-value pair separated by a colon (:).
- Key: Unique identifier in a dictionary that maps to a value; must be of immutable type.
- Mutable (dictionary): Contents of a dictionary can be changed after creation.
3. Syntax and constructs
```python
Tuple creation
tuple1 = (1, 2, 3, 4, 5)
tuple2 = ('Economics', 87, 'Accountancy', 89.6)
single = (20,) # single-element tuple must have trailing comma
Tuple assignment / unpacking
(num1, num2) = (10, 20)
(name, roll, sub) = record
Concatenation, repetition, membership, slicing
t3 = tuple1 + tuple2
t4 = tuple1 * 3
'Green' in tuple1
tuple1[2:7]
tuple1[::-1]
Dictionary creation
dict1 = {}
dict2 = {'Mohan': 95, 'Ram': 89}
dict3 = dict([('Mohan',95), ('Ram',89)])
```
Functions / Methods
```python
len(tuple1) # returns number of elements
tuple('aeiou') # creates tuple from sequence
tuple1.count(10)
tuple1.index(30)
sorted(tuple1)
min(tuple1), max(tuple1), sum(tuple1)
dict1.keys()
dict1.values()
dict1.items()
dict1.get('Ram')
dict1.update(dict2)
del dict1['Ram']
dict1.clear()
```
4. Algorithms and worked logic
Tuple unpacking (swap without temporary variable)
- Read two numbers into num1 and num2.
- Execute (num1, num2) = (num2, num1).
- Print swapped values.
Function returning multiple values (area & circumference)
- Define function that computes area and circumference.
- Return both values as a tuple: return (area, circumference).
- On calling side, unpack: area, circumference = circle(radius).
Creating tuple from user input
- Start with empty tuple
numbers = tuple().
- Loop n times, read each number and concatenate:
numbers = numbers + (num,).
- Use
max(numbers) and min(numbers).
Traversing a dictionary (two NCERT methods)
- Method 1: for key in dict1: print(key, ':', dict1[key]).
- Method 2: for key, value in dict1.items(): print(key, ':', value).
Character frequency dictionary
- Initialise empty dictionary.
- For each character in string: if already present increment count, else set count to 1.
5. Common errors and exam pitfalls
- Omitting comma after single element:
(20) is treated as int, not tuple → TypeError on len().
- Attempting item assignment on tuple:
tuple1[4] = 10 → TypeError: 'tuple' object does not support item assignment.
- Mismatch in tuple unpacking: number of variables ≠ number of elements → ValueError: not enough values to unpack.
- Using index that does not exist:
tuple1[15] or dict1['Shyam'] → IndexError / KeyError.
- Forgetting that
sorted() returns a new list (does not modify original tuple).
- Confusing dictionary keys with indices; membership (
in) works on keys only.
- Using mutable type as dictionary key (not allowed).
- Forgetting that
del dict1 removes the whole dictionary (subsequent reference causes NameError).