0% found this document useful (0 votes)
4 views

Dictionary in Python Question

The document contains a series of questions and tasks related to Python dictionaries, including functions like pop(), popitem(), setdefault(), and fromkeys(). It also provides examples of manipulating dictionaries, such as adding, removing, and modifying elements, as well as retrieving values and handling errors. Additionally, it includes programming exercises that involve creating and processing dictionaries with specific data structures.

Uploaded by

Vishal .U.S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Dictionary in Python Question

The document contains a series of questions and tasks related to Python dictionaries, including functions like pop(), popitem(), setdefault(), and fromkeys(). It also provides examples of manipulating dictionaries, such as adding, removing, and modifying elements, as well as retrieving values and handling errors. Additionally, it includes programming exercises that involve creating and processing dictionaries with specific data structures.

Uploaded by

Vishal .U.S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Question Answers:

1. Write about dictionary.


2. Write about pop() and popitem() function of dictionary.
3. Write about setdefault() function of dictionary.
4. Write about fromkeys() function of dictionary.
5. Given a dictionary d = {3:70, ‘python’: ‘fun’, 23: 44}, write python statement for the following
(i) to add an element with key as 1 and value as 2.
(ii) to add the following elements at once, 34:40, 60:10 and ‘a’: “b”
(iii) to remove the element with the key 23
(iv) to remove the last element form the dictionary.
(v) to modify the value of the element with key ‘python’ – new value to be set is ‘computer’
(vi) to remove all the elements from the dictionary.
(vii) to add an element with key as 500 and value as 600. Note: the element should get added only if
the element is not present in the dictionary. If the element is already present then it should not
modify its value.

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.

d = {“hello”: “hi”, 34: 40, [2,3]: (20, 30)} #statement 1


d[34] = 40 #statement 2
print(d[1]) #statement 3
d.setdefault(90) #statement 4
print(d.get(100)) #statement 5

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:

products = { 101: ["Laptop", "Electronics", 15, 55000],


102: ["Table", "Furniture", 30, 4500],
103: ["Pen", "Stationery", 100, 20],
104: ["Mobile Phone", "Electronics", 25, 15000],
105: ["Chair", "Furniture", 50, 2500],
106: ["Notebook", "Stationery", 200, 50],
107: ["Headphones", "Electronics", 40, 2500],
108: ["Water Bottle", "Kitchenware", 60, 350] }

Write python program for each of the following:

(i) Display the names of the product of type ‘Electronics”.


(ii) Display the product ID of the product with quantity less than 50.
(iii) Display the names of those product which are of type ‘Furniture’ and costs more than 3000.
(iv) Add the name of those products which are of type ‘Stationery’ to a list named ‘st’.
(v) Add the name, type and price of Electronics products with price more than 10000 to new list
‘electronics’.

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

15. Write the output for the following python program


LS = ["HIMALAYA", "NILGIRI", "ALASKA", "ALPS"]
D = {}

for S in LS:
if len(S) % 4 == 0:
D[S] = len(S)

for K in D:
print(K, D[K], sep="#")

16. Write the output of the code given below:


dict1={1:["Rohit",20], 2:["Siya",90]}
dict2={1:["Rahul",95], 5:["Rajan",80]}
dict1.update(dict2)
print(dict1.values())

17. Write the output of the code given below:


d={"IND":"DEL","SRI":"COL","CHI":"BEI"}
str1=""
for i in d:
str1=str1+str(d[i])+"@"
str2=str1[:-1]
print (str2)

18. Write the Output

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="*")

You might also like