Dictionaries in Python
* A dictionary represents a group of
elements arranged in the form of key-value pairs .
* In the dictionary , the first element is
considered as "key" and the immediate next element is considered as
the "value"
* The key and the associated value are
separated by the help of a colon (:) operator
* All the key-value paris in a dictionary
are inserted in curly braces { }
* One can take a dictionary by the name
"dict" that contains the employee details in the following manner :
dict_01 =
{
'Name'
: 'Gareeb' ,
'Id'
: 001 ,
'Salary'
: 0000
}
* The name of the dictionary item object in
the given example is "dict_01"
* The first element in the dictionary is a
string object called "Name" and its associated value is
"Gareeb" .
The second item in the element is
"Id" and its associated value is "001" . The third object
in the dictionary object is "Salary" and its associated value is
"0000"
* The above example is that of a dictionary
object with its identifier name as "dict_01" . The dictionary item
object is holding three values within it in the form of key and value pair .
* From this , we can get to see that there
are 3 pairs of keys and values in the dictionary which can be shown in the
below figure .
* To briefly explain the process of
creation of a dictionary object , one can write a
program in the given manner :
# creating dictionary with key-value pairs
dict_01 =
{
'Name' : 'Gareeb' ,
'Id':001 ,
'Salary':0000
}
# Here a dictionary object is created with
the following items in the form of
# key-value pairs - Name , Id and Salary
# If someone wants to retrieve the
requisite values from the dictionary then one
# can do the following to retrieve the
necessary objects
print(' Name of Employee ',dict_01['Name'])
print(' Id of Employee ',dict_01['Id'])
print(' Salary ',dict_01['Salary'])
The Code and Output for the same can be
seen in the below given screenshot: