Dictionary:
- The dictionary data type is similar to the sets but a little bit different and the dictionary are also mutable.
- It contains pair of parameters. i.e, keys and values.
- Key is used to store element names.
- Value is used to store element values.
Syntax:
Declaring dictionry:
Variable_name={}
Example:
dict={}
Declaring value into the list:
Variable_name={item_name 1: item_value 1,
item_name 2: item_value 2,
item_name n: item_value n}
Example:
std={"Roll_num":1000,'Name':'Kurshitha','Gender':'Female','mark':450}
Example program for print the dictionay of elements:
std={"Roll_num":1000, 'Name':'Kurshitha','Gender':'Female','mark':450}
print("Student name: ",std['Name'])
print("Elements in dictionary:")
print(std)
Output:
Print element name in for loop using keys() function:
std={"Roll Number":1000,'Name':'Kurshitha','Gender':'Female','Mark':450}
for i in std.keys():
print(i)
Output:

Print element values in for loop using values() function:
std={"Roll Number":1000,'Name':'Kurshitha','Gender':'Female','Mark':450}
for i in std.values():
print(i)
Output:
Print key and values in for loop using item() function:
std={"Roll Number":1000,'Name':'Kurshitha','Gender':'Female','Mark':450}
for k,v in std.items():
print(k,":",v)
Output:
No comments:
Post a Comment