PYTHON DICTIONARY
PRACTICAL QUESTIONS
GRADE XI
#to add new values into
the dictionary
d={}
n=int(input("how
many elements u want to enter"))
for i in range(n):
r=input("enter rollno")
n=input("enter name")
m=int(input("enter marks"))
d[r]=n,m
print(d)
'''
#---------------------------------------------
#to display elements of
a dictionary
d={'a001': ('teena',
65), 'a002': ('sheena', 78), 'a003': ('piyush', 87)}
print(d)
#to search a particular
key according to user's choice and display values
s=input("enter the
key which u want to search")
for i in d:
if(i==s):
print(d[i])
Dictionary
Q1 Explain the following functions in
reference to dictionary.
a. Items()
b. Keys()
c. Values()
Ans a. items()- this method returns the
content of dictionary as a list of tuples having key-value pairs.
b.
keys()-it returns the list of key values in dictionary.
c.
values()- it returns the list of values from dictionary.
Q2 Write the output of the following:
A={1:10,2:20,3:30}
B={3:35,4:40}
print(a.items())
print(a.keys())
print(a.values())
Ans:
dict_items([(1,10),(2,20),(3,30)])
dict_keys([1,2,3])
dict_values([10,20,30])
Q3 write a program to store details like book
number,author name and price in dictionary.
Ans
b={}
N=int(input(“enter number of records to
enter”))
for i in range(N):
bno=int(input(“enter book number”))
bn=input(“enter author name”)
pr=int(input(“enter price”))
t=(bn,pr)
b[bno]=t
print(b)
Q4 To
delete an element from dictionary , we can use either __________statement of
_________method.
Ans
del,pop()
Q5 What is the difference between list and
dictionary?
Ans
List Dictionary
Elements are enclosed in square brackets Elements are enclosed in curly braces
Index value is an integer only key value can be of immutable type
L=[1,2,3,5] D={1:’a’,2:’b’}
Q6 Why list can not be used as keys in
dictionary.
Ans because list are mutable
Q7
a={1:10,2.5:20,3:30,4:40,5:50}
Find errors in the following and write the
correct statement.
1. a(6)=60
2. a.len()
3.
a.clears()
4. a.gets(3)
5. a.item()
6. a.values()
7. a.keys()
8. a.del(4)
9. pop(4)
Ans
1. a[6]=60
2. len(a)
3. a.clear()
4. a.get(3)
5. a.items
6. a.values()
7. a.keys()
8. del
a[4]
9. a.pop(4)
Q8
d1={1:’one’,2:’two’,3:’three’}
d2={5:’five’,6:’six’}
write the code of the following, in reference
to the above dictionary
a. write
a for loop to print only keys of d1
Ans
for i
in d1:
print(i)
b. write
a for loop to print only values of d1
Ans
for i
in d1:
print(d1[i])
c. write
a for loop to print both keys and values of d1 in the following format.
1-----one
2-----two
Ans
for i
in d1:
print(i,”------“, d[i])
d. add
one more pair(4: “four”) in d1
Ans
d1[4]=”four”
e. modify
the value of key 2. (make it “too” in place “two”)
Ans
d1[2]=’too’
f. merge
d1 and d2 in d1.
Ans
d1.update(d2)
Q9 What do you mean by update() function in
dictionary?
Ans update() function is used to merge two
dictionaries into one dictionary.
Q10 Write the output of the following code:
a={1:10,2:20,3:30}
b={3:35,4:40}
b[5]=50
b[4]=45
print(a)
print(b)
a.update(b)
print(a)
print(b)
Ans
{1:10,2:20,3:30}
{3:35,4:40}
{1:10,2:20,3:35,4:40}
{3:35,4:40}
Q11 what is the difference between del command
and pop() function in context of dictionary?
Ans pop() method return the deleted element
while del statement does not return the deleted element.
Q12
a={1:10,2:20,3:30}
1. write
code to delete the second element using del command.
2. Write
code to delete the third element using pop() function
Ans
1. del
a[2]
2. a.pop(3)
Q13 Write the output of the following code:
a={1:10,2:20,3:30}
b={3:35,4:40}
print(1
in a) # o/p true
print(3
in b) # o/p true
print(4 in a) #
o/p false
print(max(a)) #
o/p 3
print(min(a)) #
o/p 1
print(max(b)) #
o/p 4
print(min(b)) #
o/p 3
print(sum(a)) #
o/p 6
print(sum(b)) #
o/p 7
print(len(b)) #
o/p 2
print(len(a)) #
o/p 3
Q14 what do you mean by clear() function in
reference to dictionary?
Ans clear() function removes all the elements
from dictionary
Q15 get() method returns the key or a value in
dictionary
Ans get() method returns a value for the given
key
Q16 get() method returns _________, if key
does not exist in dictionary.
Ans none
Q17 get() method takes the key or value as
argument
Ans key
Q18 Name a function which is used to create an
empty dictionary.
Ans dict()
Q19 what empty curly braces({}) shows in
python?
Ans curly braces shows empty dictionary in
python.
Q1 WAP to enter roll number and names of five
students and store the data in dictionary.
ans
d={}
for i in range(5):
r=int(input("enter
rollno"))
n=input("enter
name")
Q2 WAP to store book id, book name and price
of three books and store the data in dictionary name "dict"
sol
dict={}
for i in range(3):
b_id=int(input("enter
book id:"))
b_name=input("enter
book name:")
b_price=int(input("enter
price of book:"))
dict[b_id]=b_name,b_price
#note : by dafault the multiple values are
stored in the dictionary in the tuple form
Q3 Keys must be
______dictionary.(mutable/immutable)
Ans: immutable
Q4 Write a built in function which is used to create an empty dictionary.
Ans: dict()
Q5 Write the code to print only keys of the
following dictionary.
d={"Amit":40,"Sunil":34,"Naina":30}
Ans: d.keys()
Q6 Write the code to add the following detail
in dictionary given above.
"Ravi"-45
Ans
Q7 Write the output of the following code:
d={"Amit":40,"Sunil":34,"Naina":30}
print(d["Amit"]+d['Sunil'])
Ans 74
Q8 Name the function which is used to return a
value of the given key.
Ans get()
Q9 Write two differences between list and
dictionary.
Q10 Write the output of the following:
m=_{1:"jan",2:"jun",3:"aug"}
print(m[1])
print(m["jan"])
Ans : print(m["jan"])# can't search
according to the value
Q11 Find errors in the following statement and
write the correct code:
n={1:10,2:20,3:30,4:40,5:50}
(a) print(n.item())
(b) print(n.len())
(c) print(pop(5))
Q12 Write one similarity and one difference
between del statement and pop() function in reference to dictionary.
d={}
#entry
s=int(input("enter how many
records"))
for i in range(s):
r=int(input("enter rollno"))
n=input("enter name")
d[r]=n
#display
for i in d:
print(i,d[i])
#deletion
a=int(input("enter rollno which u want to
delete"))
del d[a]
for i in d:
print(i,d[i])
#update
a=int(input("enter rollno which u want to
update"))
v=input("enter the value")
d[a]=v
for i in d:
print(i,d[i])
#search
a=int(input("enter rollno which u want to
display"))
for i in d:
if
i==a:
print(i,d[i])
#deletion based searching
a=int(input("enter rollno which u want to
delete"))
if a in d:
print(i,d[i])
d.pop(a)
break
else:
print("not found")
break
a=0
while a!=1:
print("1. add information")
print("2. display all information")
print("3. search a particular info")
print("4. delete a particular info")
print("5. modification a particular info")
print("6. exit")
ch=int(input("enter your choice"))
d={}
if(ch==1):
n=int(input("how many elements u want to enter"))
for i in range(n):
r=input("enter rollno")
n=input("enter name")
m=int(input("enter marks"))
d[r]=n,m
print(d)
elif(ch==2):
for i in d:
print(i,d[i])
elif(ch==3):
s=input("enter the key which u want to search")
for i in d:
if(i==s):
print(d[i])
elif(ch==4):
s=input("enter the key which u want to search")
for i in d:
if(i==s):
d.pop(s)
print(d)
elif(ch==5):
s=input("enter the key which u want to search")
v=int(input("enter the value"))
d[s]=v
print(d)
elif(ch==6):
a=1
Comments
Post a Comment