Computer Science - Informatics Practices Class XI/XII Python Dictionary Solved Test Part 5

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
  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)

                 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
  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)

No comments:

Post a Comment

Most Recently Published

File Handling Test 6

 File Handling Test 6 Q1 . Do we need to use the close() function when we use 'with' statement to open a file? Q2. Which mode you wi...

CS - IP Assignment/Worksheet