Computer Science

Predict the output:

arr = {}
arr[1] = 1 
arr['1'] = 2 
arr[1] += 1 
sum = 0
for k in arr:
  sum += arr[k]
print(sum)

Python Dictionaries

13 Likes

Answer

Output
4
Explanation

This python program computes the sum of values of items in the dictionary.

The arr dictionary is created and initialized with two key-value pairs, arr[1] = 1 and arr['1'] = 2. After that, the value of arr[1] is incremented by 1. At this point arr = {1: 2, '1': 2}.

The program iterates through each key k in the arr dictionary and adds the value arr[k] to the sum variable.

The final value of the sum variable is printed to the console.

Answered By

6 Likes


Related Questions