Page 279 - CTS - CSA TP - Volume 2
P. 279
COMPUTER SOFTWARE APPLICATION - CITS
‘grade’: ‘A’
}
# Accessing dictionary elements
print(“Name:”, student[‘name’])
print(“Age:”, student[‘age’])
print(“Grade:”, student[‘grade’])
Explanation:
• A dictionary named student is created with keys (‘name’, ‘age’, ‘grade’) and corresponding values.
• Elements of the dictionary are accessed using keys, and their values are printed.
Output:
TASK 2: Dictionary Operations
student = {
‘name’: ‘SreeHari’,
‘age’: 20,
‘grade’: ‘A’
}
# Modifying dictionary
student[‘age’] = 21
student[‘course’] = ‘Computer Science’
# Deleting a key-value pair
if’grade’in student:
del student[‘grade’]
# Iterating through dictionary items
for key, value instudent.items():
print(f”{key}: {value}”)
Explanation:
• The dictionary is modified by updating the ‘age’ and adding a new key ‘course’.
• A key-value pair (‘grade’) is deleted using the del keyword.
• A loop iterates through the dictionary items, printing each key-value pair.
Output :
264
CITS : IT & ITES - Computer Software Application - Exercise 128 CITS : IT & ITES - Computer Software Application - Exercise 128