Select the output for the given Python code from the following options…

2023

Select the output for the given Python code from the following options D1={1:2,2:3,3:4} D2=D1.get(1,2) print(D2)

  1. A.

    2 / 2

  2. B.

    3 / 3

  3. C.

    [2, 3] / [2, 3]

  4. D.

    {1:2,2:3} / {1:2,2:3}

Attempted by 1671 students.

Show answer & explanation

Correct answer: A

Answer: 2

Explanation:

  • D1 is a dictionary mapping 1 → 2, 2 → 3, and 3 → 4.

  • The get method has the form get(key, default). It returns the value for the given key if present; otherwise it returns the default.

  • Here the call is D1.get(1, 2). Key 1 exists in D1 with value 2, so get returns 2 (the default is ignored).

  • Therefore print(D2) outputs 2.

Explore the full course: Rssb Senior Computer Instructor