Robotics & Artificial Intelligence
What will be the output of the following Python code?
x = {1: 'apple', 2: 'banana'}
x[3] = "cherry"
print(x)
Getting Started
1 Like
Answer
{1: 'apple', 2: 'banana', 3: 'cherry'}
Working
In a Python dictionary, values are stored using keys. The statement x[3] = "cherry" means that the value "cherry" is assigned to the key 3. This adds a new key–value pair to the dictionary, which is then displayed when the dictionary is printed.
Answered By
2 Likes