Professional Documents
Culture Documents
I037 - Manas Patel Experiment07
I037 - Manas Patel Experiment07
I037 - Manas Patel Experiment07
PRACTICAL 7
Problem Statement: Write Python program to
1. Demonstrate working with dictionaries in python (create, add, update, print, length, pop,
copy).
4. apply dictionary comprehension to convert the item prices of a shopping list from dollars
to pounds.
5. apply dictionary comprehension to write odd or even as per the age value.
PRACTICAL 7
1|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
3. Multiple Choice
1. What will list(d.keys()) return?
d = {'A': 1, 'B': 2, 'C': 3}
a. [1,2,3]
d. [‘A’,’B’,’C’]
b. ‘A’,’B’,’C’
c. [A, B, C]
d. [‘A’,’B’,’C’]
2. How would one delete the entry with key 'A'?
d = {'A': 1, 'B': 2, 'C': 3}
a. d.delete(‘A’:1)
c. del d[‘A’]
b. d.delete(‘A’)
c. del d[‘A’]
d. del d[‘A’:1]
3. What is the result of the following code?
d = {}
c. 5
d[1] = 1
d['1'] = 2
2|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
d[1] = 3
sum = 0
for i in d:
sum += d[i]
print(sum)
a. 2
b. 3
c. 5
d. 6
4. What is the result of the following code?
course = {'1': 'Physics',
'2': 'Chemistry',
'3': 'Biology'}
print(course['1']) a. Physics
a. Physics
b. Chemistry
c. None
d. KeyError
5. What is the result of the following code?
course = {'1': 'Physics',
'2': 'Chemistry',
'3': 'Biology'}
print(course['1']) a. Physics
a. Physics
b. Chemistry
c. None
d. KeyError
3|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
#Print dictionary
print(college)
#length of dictionary
print('Length of dictionary:',len(college))
print('Length of name:',len(college['Name']))
4|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
print('Length of dictionary:',len(college))
del college
#print(college) Error bcoz dictionary already deleted
OUTPUT:
5|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Q.2
CODE:
#SORTING
d={}
n=int(input('Enter number of items:'))
for i in range(n):
name=input('Enter product name:')
price=float(input('Enter product price:'))
d[name]=price
print(d)
#Sorting keys
print(sorted(d.keys()))
#Sorting values
print(sorted(d.values()))
#Sorting items
print(sorted(d.items()))
6|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
OUTPUT:
Q.3
CODE:
#COMBINE TWO DICTIONARY
d1={'a':100,'b':200,'c':300}
d2={'a':300,'b':200,'d':400}
d3={}
d4={}
print('Dictionary 1:',d1)
print('Dictionary 2:',d2)
7|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
else:
d3[key]=d1[key]
print('Dictionary 3:',d3)
#METHOD-2
d4.update(d1)
d4.update(d2)
print('Dictionary 4:',d4)
OUTPUT:
Q.4
CODE:
#DICTIONARY COMPHREHENSION
old_price={'Table':10,'Coffee':2.5,'Chocolate':4,'Bread':1.45}
dol_pnd=0.76
dol_rup=80
new_pricep={x:v*dol_pnd for (x,v) in old_price.items()}
new_pricer={x:v*dol_rup for (x,v) in old_price.items()}
8|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
print('PRICES IN DOLLAR:',old_price)
print('PRICES IN POUNDS:',new_pricep)
print('PRICES IN RUPEES:',new_pricer)
#DICTIONARY COMPHREHENSION TO ADD ODD AND EVEN AS PER AGE
VALUE
emp={'John':38,'Sarah':25,'Gary':48,'Pat':22}
nm_emp={x:('Odd' if y%2==1 else 'Even') for (x,y) in emp.items()}
print('EMPLOYEES: ',emp)
print('EMPLOYEE UPDATED: ',nm_emp)
OUTPUT:
c. Conclusion (Learning Outcomes): It can be concluded that one can use dictionary to store
data in key-value manner. The learning from the practical was about introducing dictionary,
creating dictionary, operations like update, del, pop and popitems on dictionary were
performed and loop was also used on dictionary. Dictionary also has comprehension like
list to create dictionary using conditions.
9|Page