Computer Science - Informatics Practices Class XI/XII Python Dictionary Solved Test Part 4
Q1. 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.
b. Write a for loop to print only values of d1.
c. Write a for loop to print both keys and values of d1 in the following format.
1-------One
2------Two
d. Add one more pair (4 : "Four") in d1
e. Modify the value of key 2. (make it "Too" in place "Two")
f. Merge d1 and d2 in d1.
Q2. What do you mean by update() function in dictionary?
Q3. Write the output of the following code :
Q5. a={1:10,2:20,3:30}
- Write code to delete the second element using del command.
- Write code to delete the third element using pop() function.
a={1:10,2:20,3:30}
SOLUTION
Q1. 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.
b. Write a for loop to print only values of d1.
c. Write a for loop to print both keys and values of d1 in the
following format.
1-------One
2------Two
d. Add one more pair (4 : "Four") in d1
e. Modify the value of key 2. (make it "Too" in place "Two")
f. Merge d1 and d2 in d1.
print(i)
b. for i in d1:
print(a[i])
c. for i in d1:
print(i,"------",a[i])
d. d1[4]="Four"
e. d1[2] = 'Too'
f. d1.update(d2)
Q2. What do you mean by update() function in dictionary?
Ans. update() function is used to merge two dictionaries
into one dictionary
Q3. Write the output of the following code :
Q5. a={1:10,2:20,3:30}
- Write code to delete the second element using del command.
- Write code to delete the third element using pop() function.
a={1:10,2:20,3:30}
No comments:
Post a Comment