You are on page 1of 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET- 2.3
Student Name: Harsh Raj UID: 21BCS8426
Branch: CSE Section/Group: 719/A
Semester: 4th Date of Performance: 11/04/23
Subject Name: Programming in Python Lab Subject Code: 21CSP-259

1. Aim: Program to demonstrate creation, accessing of dictionary and apply different


kinds of operations on them.
1. Create a simple dictionary with 3 elements in it and perform the following:
a. Write the Python script to check whether a given key exists in a dictionary or not.
b. Write the Python script to update the value of any key.
c. Write the Python method to delete any random element from the given dictionary.
2. Write the Python program to explain the concept of iteration over dictionaries printing
only the values of the keys.
3. Create a nested dictionary with total 2 main keys storing a nested dictionary with 2
elements as the value and perform the following:
a. Print the value of the first key.
b. Update the value of the first key.
c. Delete the element from the dictionary.

2. Source Code:
1. a.
d1={'Name':'Harsh','UID':'21BCS8426', 'Section':719}
'''
print(d1['Name']) #METHOD- 1
print(d1['UID']) #given key exists in dictionary or not.
print(d1['Section'])
print(d1['UIDS'])
'''
print(d1.get('Name'))
print(d1.get('UID')) #METHOD-2
print(d1.get('Section'))
print(d1.get('UIDS')) #given key exists in dictionary or not.
b.
d1={'Name':'Harsh','UID':'21BCS8426', 'Section':719}
d1['Section'] = '719/A'
print(d1)

c.
d1={'Name':'Harsh','UID':'21BCS8426', 'Section':719}
del d1['Name']
print(d1)

2.
d1={'Name':'Harsh','UID':'21BCS8426', 'Section':719}
for i in d1.keys():
print(d1[i])
3. a.

d1={719 :{'Name':'Harsh', 'UID':'21BCS8426'}, 720 :{'Name':'Kumar',


'UID':'21BCS8588'}}
print(d1[719])

b.

d1={719 :{'Name':'Harsh', 'UID':'21BCS8426'}, 720 :{'Name':'Kumar',


'UID':'21BCS8588'}}
d1[719] = 'Raj'
print(d1)

c.

d1={719 :{'Name':'Harsh', 'UID':'21BCS8426'}, 720 :{'Name':'Kumar',


'UID':'21BCS8588'}}
del d1[720]
print(d1)
3. Screenshot of Outputs:

1. a.

b.

c.
2.

3. a.

b.

c.

You might also like