What will be the output of the following Python function? print(len(['PM', 2,…
2026
What will be the output of the following Python function?
print(len(['PM', 2, False]))
Answer: A. 3 — ConceptIn Python, len() returns the number of top-level elements in a collection. For a list, each comma-separated entry counts as one element, regardless of…
- A.
3
- B.
2
- C.
4
- D.
Error
Attempted by 437 students.
Show answer & explanation
Correct answer: A
Concept
In Python, len() returns the number of top-level elements in a collection. For a list, each comma-separated entry counts as one element, regardless of whether it is a string, number, or Boolean value.
Application
Read the list literal: ['PM', 2, False].
Identify its top-level entries: 'PM', 2, and False.
Count those entries: 1 + 1 + 1 = 3.
Cross-check
The string 'PM' is one list element; len() does not count its two characters separately when measuring the outer list. Python also permits mixed data types in one list, so this expression does not raise a type error.
Result
Therefore, print(len(['PM', 2, False])) displays 3.