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
Computer Science,
10 days ago
English,
10 days ago
English,
21 days ago
Social Sciences,
8 months ago
English,
8 months ago
Biology,
8 months ago