I037 - Manas Patel Experiment07

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

PRACTICAL 7
Problem Statement: Write Python program to
1. Demonstrate working with dictionaries in python (create, add, update, print, length, pop,
copy).

2. sort (ascending and descending) a dictionary by keys, values and items.

3. combine two dictionary adding values for common keys.


d1={'a':100,'b':200,'c':300}
d2={'a':300,'b':200,'d':400}

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

Sequence: Dictionary (create, access, update, delete)


Roll No.: I037 Name: Manas Hiteshbhai Patel
Prog/Yr/Sem: MBA Tech. IT 2nd Yr Sem-III Batch: A2
Date of Experiment: 17-09-2022 Date of Submission:

a. Questions based on Experiment Scenario:


1. Describe the features of dictionary.
Ans.
• Dictionaries are unordered: it contains key-value pairs but does not possess an order
for pair. It is an unordered collection of key-value pairs.
• Keys are unique: Dictionary keys are unique as they are used to access values in
dictionary. Dictionary does not allow creating duplicate key but if you make duplicate
key it will not warn you. It will overwrite the value for that key.
• Keys must be immutable: String and numbers are used data types for dictionary keys.
• Dictionary comprehension: It is method of writing dictionary with writing key-value
pair in curly braces.

1|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III
2. Describe various dictionary methods.
Ans.
• clear(): Removes all element from dictionary
• copy(): Returns a copy of dictionary
• fromkeys(): returns dictionary with specified keys and value
• get(): returns the value of specified key
• items(): returns list containing tuple for each key value pair
• keys(): returns list containing dictionary keys
• popitem(): removes last inserted key-value pair
• pop(): removes element with specified key
• update(): updates the dictionary

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

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

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

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III
b. Program Code along with Sample Output:
Q.1
CODE:
#Dictionary Manipulation(create, add, delete, update, print)
#Creating a dictionary
college={'Name':'MPSTME','College':'NMIMS','Id':'NMIMS_MPSTME'}

#Print dictionary
print(college)

#length of dictionary
print('Length of dictionary:',len(college))
print('Length of name:',len(college['Name']))

#Adding item in dictionary


college['Location']='Mumbai'
print(college)

#Updating item in dictionary


college['Location']='Shirpur'
print(college)

#Removing selected item in dictionary


college.pop('Id')
print(college)

#Removing random item in dictionary


college.popitem()
print(college)

4|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

print('Length of dictionary:',len(college))

#Empty the dictionary


college.clear()
print(college)

del college
#print(college) Error bcoz dictionary already deleted

#Creating empty dictionary


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)

OUTPUT:

5|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

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 keys in reverse


print(sorted(d.keys(),reverse=True))

#Sorting values
print(sorted(d.values()))

#Sorting values in reverse


print(sorted(d.values(),reverse=True))

#Sorting items
print(sorted(d.items()))

#Sorting items in reverse


print(sorted(d.items(),reverse=True))

6|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

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)

#Picking common items


#METHOD-1
d3=d2.copy()

for key in d1.keys():


if key in d2.keys():
d3[key]=d1[key]+d2[key]

7|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

else:
d3[key]=d1[key]

print('Dictionary 3:',d3)

#METHOD-2
d4.update(d1)
d4.update(d2)
print('Dictionary 4:',d4)

for key in d1.keys():


if key in d2.keys():
d4[key]=d1[key]+d2[key]
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

Course: Python Programming


PROGRAMME: B.(Tech.) Computer Science and Engineering (Cybersecurity),
MBA(Tech.) IT
Second Year AY 2022-2023 Semester: III

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

You might also like