Class XI · Chapter 10Unit 2, Python Programming (45 marks)6 min read
Share:WhatsAppLinkedIn

Chapter 10: Tuples and Dictionaries

CBSE Unit: Unit 2, Python Programming (45 marks) Marks Weightage: ~6-8 marks Priority: CRITICAL, tuples and dictionaries both tested frequently


Part A: Tuples

10.1 Introduction to Tuples

  • Ordered, immutable sequence of elements, Can contain mixed data types
  • Enclosed in parentheses (), separated by commas, Indices start from 0
t1 = (1, 2, 3, 4, 5)               # integers
t2 = ('Eco', 87, 'Acc', 89.6)      # mixed
t3 = (10, 20, 30, [40, 50])        # with list
t4 = (1, 2, (10, 20))              # nested tuple
t5 = ()                             # empty tuple

Single element tuple: Must have a trailing comma

t = (20,)       # tuple with one element
t = (20)        # NOT a tuple -- this is just integer 20!

Tuple without parentheses (packing):

t = 1, 2, 3    # treated as tuple by default

10.2 Accessing and Slicing

t = (10, 20, 30, 40, 50)
t[0]       # 10
t[-1]      # 50
t[1:4]     # (20, 30, 40)
t[::-1]    # (50, 40, 30, 20, 10)

10.3 Tuple Operations

Operation Example Result
Concatenation (1,2) + (3,4) (1,2,3,4)
Repetition (1,2) * 2 (1,2,1,2)
Membership 3 in (1,2,3) True
Length len((1,2,3)) 3
Max/Min max((1,2,3)) 3
Sum sum((1,2,3)) 6

10.4 Tuple is Immutable

t = (1, 2, 3)
t[0] = 10      # TypeError! Cannot modify
  • Cannot add, remove, or change elements, But a mutable element inside tuple (like a list) can be modified:
t = (1, 2, [3, 4])
t[2][0] = 30     # Valid! t = (1, 2, [30, 4])

10.5 Tuple Methods

Method Description
count(x) Number of occurrences of x
index(x) Index of first occurrence of x

Only two methods because tuples are immutable.

10.6 Tuple Assignment (Unpacking)

(a, b, c) = (10, 20, 30)    # a=10, b=20, c=30
a, b = b, a                  # Swap values!

10.7 Tuple vs List

Feature Tuple List
Syntax () []
Mutability Immutable Mutable
Methods count(), index() only Many (append, remove, sort, etc.)
Speed Faster Slower
Use case Fixed data Data that changes

Part B: Dictionaries

10.8 Introduction to Dictionaries

  • Unordered collection of key-value pairs
  • Enclosed in curly braces {}
  • Each key is unique and immutable (string, number, tuple), Values can be any data type and can be duplicated
d1 = {'name': 'Amit', 'age': 17, 'marks': 95}
d2 = {1: 'one', 2: 'two', 3: 'three'}
d3 = {}    # empty dictionary
d4 = dict()  # empty dictionary

10.9 Accessing Values

d = {'name': 'Amit', 'age': 17}
d['name']          # 'Amit'
d.get('name')      # 'Amit'
d.get('class', 'NA')  # 'NA' (default if key not found)
d['class']         # KeyError! (key doesn't exist)

10.10 Dictionary Operations

Adding/Modifying

d = {'name': 'Amit'}
d['age'] = 17          # Add new key-value pair
d['name'] = 'Sumit'    # Modify existing value

Deleting

del d['age']           # Delete specific key
d.pop('name')          # Remove and return value
d.clear()              # Remove all items

Membership (checks keys only)

'name' in d            # True (checks if key exists)

Traversing

d = {'a': 1, 'b': 2, 'c': 3}

for key in d:
    print(key, d[key])          # prints keys and values

for key in d.keys():
    print(key)                   # prints keys

for value in d.values():
    print(value)                 # prints values

for key, value in d.items():
    print(key, ":", value)       # prints key-value pairs

10.11 Dictionary Methods

Method Description Example
keys() Returns all keys d.keys()
values() Returns all values d.values()
items() Returns all key-value pairs as tuples d.items()
get(key, default) Returns value for key; default if not found d.get('x', 0)
update(dict2) Merge dict2 into d d.update({'x': 10})
pop(key) Remove key and return value d.pop('name')
clear() Remove all items d.clear()
copy() Return shallow copy d2 = d.copy()
len() Number of key-value pairs len(d)
sorted() Returns sorted list of keys sorted(d)

10.12 Dictionary vs List

Feature Dictionary List
Access By key By index
Order Unordered (Python 3.7+ maintains insertion order) Ordered
Syntax {key: value} [elements]
Duplicate keys Not allowed N/A

Programs/Code Examples

Create Dictionary from Two Lists

keys = ['name', 'age', 'city']
values = ['Amit', 17, 'Delhi']
d = dict(zip(keys, values))
print(d)    # {'name': 'Amit', 'age': 17, 'city': 'Delhi'}

Count Character Frequency

s = input("Enter string: ")
d = {}
for ch in s:
    if ch in d:
        d[ch] += 1
    else:
        d[ch] = 1
print(d)

Histogram of List Values

L = [1, 2, 3, 2, 1, 3, 3, 1, 1]
freq = {}
for item in L:
    freq[item] = freq.get(item, 0) + 1
print(freq)    # {1: 4, 2: 2, 3: 3}

Common Board Exam Question Patterns

  1. Find output of tuple/dictionary operations (2-4 marks)
  2. Write a program using dictionaries (3-5 marks): frequency count, merge
  3. Difference between tuple and list, dictionary and list (2 marks)
  4. Dictionary methods (2 marks): get(), keys(), values(), items()
  5. Single element tuple (1 mark): Why (5,) not (5)
  6. Immutability of tuples (1-2 marks): Why modification fails
  7. Dictionary traversal (2-3 marks): Using for loop with keys/values/items

Key Points Students Miss

  1. Single-element tuple needs a comma: (5,) not (5), (5) is just integer 5
  2. Tuples have only two methods: count() and index()
  3. Dictionary keys must be immutable (str, int, float, tuple OK; list NOT OK)
  4. d['key'] raises KeyError if not found; d.get('key') returns None
  5. d.get('key', default) returns default if key not found, very useful
  6. Dictionary keys are unique, assigning same key overwrites the value
  7. in operator with dictionary checks keys, not values
  8. Tuple is faster than list because it's immutable (less overhead)
  9. del can delete dictionary key or entire dictionary
  10. Dictionary items() returns key-value pairs as list of tuples

Test Your Knowledge

Take a quick quiz on this chapter

Start Quiz →

Prefer watching over reading?

Subscribe for free.

Subscribe on YouTube