Page 104 - tp_Modula_v2.0
P. 104
On running the above program, you will get the following output:
Output
{'Name': 'A', 'Rollno': 1}
{'Name': 'B', 'Rollno': 2, 'Marks': [15, 47, 54]}
{'Name': 'C', 'Rollno': 3}
{'Name': 'D', 'Rollno': 4}
ACCESSING A DICTIONARY
To access the elements of a dictionary, you need the key defined in the key:value pairs. You can
also use the get( ) method for fetching the value of a particular key of a dictionary.
Syntax to access a dictionary:
<dictionary-name>[<key>]
Program 2: To access a dictionary.
Program2.py
File Edit Format Run Options Window Help
dict1={'Name': 'A', 'Rollno': 1}
print(dict1)
dict2={'Name': 'B', 'Rollno': 2, 'Marks': [15, 47, 54] }
print(dict2)
print(dict1['Name'])
print(dict1['Rollno'])
print(dict2.get('Name'))
print(dict2.get('Rollno'))
print(dict2.get('Marks'))
On running the above program, you will get the following output:
Output
{'Name': 'A', 'Rollno': 1}
{'Name': 'B', 'Rollno': 2, 'Marks': [15, 47, 54]}
A
1
B
2
[15, 47, 54]
PYTHON METHODS
Python provides various built-in dictionary methods. The following table describes explanation
of these methods.
Method Explanation
pop(key) Removes an element whose key is given
popitem( ) Removes an arbitrary element and returns its value
102 Touchpad MODULAR (Version 2.0)

