RG's E-Learning Centre · Book a free demo class · WhatsApp +91-9379416374

Dictionary operations: Class 12 practice

Dictionaries map keys to values. Read the method, follow the three worked examples, then try the six questions.

What you should know first

  • Lists

The idea behind dictionary operations

Dictionaries map keys to values. d[k] = v adds or updates; d.get(k, default) avoids errors for missing keys; len(d) counts keys; iterating over d gives keys.

Step-by-step method

  1. Track the dictionary after each statement.
  2. Updating an existing key replaces the value.
  3. get() returns the default if the key is missing.

Worked example 1 (Basic)

d = {'a': 13, 'b': 19, 'c': 16} d['b'] = 31 print(d['b'], len(d))

  1. 'b' is updated to 31
  2. Still 3 keys
  3. Answer: 31 3

Worked example 2 (Standard)

d = {'a': 17, 'b': 1, 'c': 19} print(d.get('z', 40) + d['a'])

  1. 'z' missing → 40
  2. 40 + 17 = 57
  3. Answer: 57

Worked example 3 (Challenge)

d = {'a': 6, 'b': 5, 'c': 12, 'd': 19} s = 0 for k in d: s += d[k] print(s)

  1. Iterating gives keys; add each value
  2. = 42
  3. Answer: 42

Common mistakes and misconceptions

  • Thinking duplicate keys are kept
  • Expecting d[k] on a missing key to return None

Practice questions

  1. (Basic) d = {'a': 5, 'b': 14, 'c': 18} d['b'] = 24 print(d['b'], len(d))
  2. (Basic) d = {'a': 12, 'b': 16, 'c': 13} d['b'] = 28 print(d['b'], len(d))
  3. (Standard) d = {'a': 8, 'b': 9, 'c': 4} print(d.get('z', 32) + d['a'])
  4. (Standard) d = {'a': 4, 'b': 1, 'c': 12} print(d.get('z', 40) + d['a'])
  5. (Challenge) d = {'a': 19, 'b': 16, 'c': 2, 'd': 16} s = 0 for k in d: s += d[k] print(s)
  6. (Challenge) d = {'a': 19, 'b': 16, 'c': 8, 'd': 12} s = 0 for k in d: s += d[k] print(s)

Answer key with full working

  • Q1: 'b' is updated to 24 → Still 3 keys → Answer: 24 3
  • Q2: 'b' is updated to 28 → Still 3 keys → Answer: 28 3
  • Q3: 'z' missing → 32 → 32 + 8 = 40 → Answer: 40
  • Q4: 'z' missing → 40 → 40 + 4 = 44 → Answer: 44
  • Q5: Iterating gives keys; add each value → = 53 → Answer: 53
  • Q6: Iterating gives keys; add each value → = 55 → Answer: 55

Syllabus note

This skill is listed under "Python Revision Tour" for Class 12 in the NCERT/CBSE books. ICSE, IGCSE, IB and state boards may teach it in a different chapter or year.

Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.

Related practice and notes

Frequently asked questions

Which chapter is dictionary operations in?

"Python Revision Tour", Class 12 Computer Science (NCERT/CBSE). Other boards may place it differently.

How were the answers checked?

Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.

Book a free demo class