What does the `len()` function return when called on the Python dictionary `d = {'a': 1, 'b': 2, 'c': 3}`?

Answer and explanation

Correct answer: B. 3

  1. Step 1: The built-in `len()` function on a dictionary returns the number of key-value pairs stored in it.
  2. Step 2: The dictionary has three distinct keys (`'a'`, `'b'`, `'c'`), so `len(d)` evaluates to 3.
Answer
3.
Why not the others: '6' counts keys and values separately; '1' returns the first key's value; 'Error' assumes dictionaries have no length property.

Practise more Python Programming, Data Structures, Algorithms, Networks and Cybersecurity.

Free questions with an explanation for every one, and full timed Saudi Secondary School Certificate mock tests.