Computer Science, asked by Dida1058, 21 days ago

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 Equestriadash
5

There could be two ways.

Way 1:

>>>\ \tt ClassRoll\ =\ \{1:'Reena',\ 2:'Rakesh',\ 3:'Zareen'\}\\

Way 2:

>>>\ \tt ClassRoll\ =\ dict()\\>>>\ ClassRoll[1]\ =\ 'Reena'\\>>>\ ClassRoll[2]\ =\ 'Rakesh'\\>>>\ ClassRoll[3]\ =\ 'Zareen'

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