Dictionary in Python Question
Dictionary in Python Question
6. Given a dictionary d = {3:70, ‘python’: ‘fun’, 23: 44}, write python statement for the following
(i) to display the value of the element with the key 3.
(ii) to display all the keys of the dictionary.
(iii) to display all the values from the dictionary.
(iv) to display all the elements in form of list of tuple, with each tuple containing two elements
indicating key and its corresponding value.
(v) to display the value of the element with the key “a”, if the element is not present then it should
display “hello”.
7. Given a dictionary d = {1: 2, ‘python’ : ‘fun’, 30:40, 100:200}. Write the output for the following
statements
(i) print(d)
(ii) print(d[3])
(iii) print(d[100])
(iv) print(d.values())
(v) print (d.items())
(vi) print(d.keys())
(vii) print(d.get(100))
(viii) print(d.get(123))
(ix) print(d.get(111,222))
(x) print(d.pop(‘python’))
(xi) print(d.popitem())
(xii) print(d[100]**2)
8. Consider the following python program, verify if all the statements of the program would execute
without error.. If any of the statement would produce error, then list it and mention the reason for error.
If none of the statement produce error, then write the output.
9. Create a dictionary with 5 elements with each element containing data in form of
name:phoneNumber
10. Consider a dictionary d containing data in from of name:phoneNumber, write a python program
that would get a name as input and print its phoneNumber. Also, if the Name is not found in the
dictionary, then it should print “Name not found”.
11. Write a python program that would get name and age of 5 persons and add them to a dictionary
with the structure name:age.
12. Write a python program that would print names of the students who have scored more than 80 from
the dictionary named “marks” containing data of the form Name:Marks.
13. Consider the following dictionary, containing data with the following struture.
{productID : [productName, producttype, quantity, price]}
Eg Dictionary:
14. Write a python program that displays the corresponding grade obtained by the students according to
the following grading rules:
Average of Eng, Math, Science Grade
=90 A
<90 but >=60 B
<60 C
For example: Consider the following dictionary
S={"AMIT": [92, 86, 64], "NAGMA": [65, 42, 43], "DAVID": [92, 90, 88]}
The output should be:
AMIT - B
NAGMA - C
DAVID - A
for S in LS:
if len(S) % 4 == 0:
D[S] = len(S)
for K in D:
print(K, D[K], sep="#")
S = "LOST"
L = [10, 21, 33, 4]
D = {}
for I in range(len(S)):
if I % 2 == 0:
D[L.pop()] = S[I]
else:
D[L.pop()] = I + 3
for K, V in D.items():
print(K, V, sep="*")