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

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 :


Q4. What is the difference between del command and pop() function in context of 
      dictionary?

Q5.  a={1:10,2:20,3:30}

  1. Write code to delete the second element using del command.
  2. Write code to delete the third element using pop() function.
Q6. Write the output of the following code.

                a={1:10,2:20,3:30}

        b={3:35,4:40}
        print(1 in a)
        print(3 in b)
        print(4 in a)
        print(max(a))
        print(min(a))
        print(max(b))
        print(min(b))
        print(sum(a))
        print(sum(b))
        print(len(b))
        print(len(a))

Q7. What do you mean by clear() function in reference to dictionary?

Q8. get() method returns ______(key or value) in dictionary.?

Q9. get() method returns ______, if key does not
exist in dictionary.

Q10. get() method takes the key or value as
argument.

                          

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.

Ans. a. for i 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 :



Ans. {1:10,2:20,3:30}
{3:35,4:40}
{1:10,2:20,3:35,4:40}
{3:35,4:40}
Q4. What is the difference between del command and pop()
function in context of dictionary?
Ans. pop() method return the deleted element while del statement
does not return the deleted element.

Q5.  a={1:10,2:20,3:30}

  1. Write code to delete the second element using del command.
  2. Write code to delete the third element using pop() function.
Ans 1. del(a[2])
2. a.pop(3)
Q6. Write the output of the following code.

                a={1:10,2:20,3:30}

        b={3:35,4:40}
        print(1 in a)
        print(3 in b)
        print(4 in a)
        print(max(a))
        print(min(a))
        print(max(b))
        print(min(b))
        print(sum(a))
        print(sum(b))
        print(len(b))
        print(len(a))
Ans.
        True         True         False         3         1         4         3         6         7         2         3
Q7. What do you mean by clear() function in
reference to dictionary?
Ans. clear() function removes all the elements from dictionary.

Q8. get() method returns the key or a value
in dictionary.
Ans. get() method returns a value for the given key.

Q9. get() method returns ______, if key does not
exist in dictionary.
Ans. None

Q10. get() method takes the key or value as
argument.
Ans. key


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