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. 3ConceptIn 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…

  1. A.

    3

  2. B.

    2

  3. C.

    4

  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

  1. Read the list literal: ['PM', 2, False].

  2. Identify its top-level entries: 'PM', 2, and False.

  3. 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.

Explore the full course: Rssb Senior Computer Instructor

Loading lesson…