Write a Python statement to declare a Dictionary named ClassRoll with Keys as 1, 2, 3 and corresponding values as ‘Reena’, ‘Rakesh’, ‘Zareen’ respectively.
Answers
Answered by
5
There could be two ways.
Way 1:
Way 2:
In the first method, we directly assign the values to the variable, using curly brackets, making it a dictionary.
In the second method, we first decide the data type of the variable ClassRoll and then assign values in to it.
A dictionary is a data type in Python that enables a key-value relationship. All dictionaries are enclosed in curly brackets. The format of a dictionary is as follows:
- Dictionary_name = {Key1:Value1, Key2:Value2, Key3:Value3, ... }
In a dictionary, the elements can be accessed using their keys, that act as indexes.
Dictionary_name[Key1] would return the value corresponding to Key1, i.e., Value1. The same goes for all the other keys. If an unknown key has been called, it would render an error.
Similar questions
Biology,
25 days ago
English,
1 month ago
Social Sciences,
9 months ago
English,
9 months ago
Biology,
9 months ago