Computer Science - Informatics Practices Class XI/XII Python Dictionary Solved Test Part 5
Q1. Explain the following functions in reference to dictionary.
a. items()
b. keys()
c. values()
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())
Q3. Write a program to store details like book
number, author name and price in dictionary.
Q4. To delete an element from dictionary, we can
use either _______ statement of ______ method.
Q5. What is the difference between list and
dictionary?
Q6. Why list can not be used as keys in dictionary?
Q7. a={1:10,2.5:20,3:30,4:40,5:50}
Find errors in the following and write the correct
statement
- a(6) = 60
- a.len()
- a.clears()
- a.gets(3)
- a.item()
- a.valueS()
- a.keyS()
- a.del(4)
- pop(4)
SOLUTIONS
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 book 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?
List | Dictionary |
Elements are enclosed in square brackets | Elements are enclosed in curly braces |
Index value is an integer only | Index value can be of any type. |
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
- a(6) = 60
- a.len()
- a.clears()
- a.gets(3)
- a.item()
- a.valueS()
- a.keyS()
- a.del(4)
- pop(4)
Ans.
- a[6]=60
- len(a)
- a.clear()
- a.get(3)
- a.items
- a.values()
- a.keys()
- del a[4]
- a.pop(4)
No comments:
Post a Comment