Showing posts with label Dictionary. Show all posts
Showing posts with label Dictionary. Show all posts

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)

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


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

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


Q1. Can we add new values in an existing dictionary?
Q2. Write a code to add the following key-value to a given dictionary.
    a = {'Class':'VI','Sec':'B','Rollno' : 1}
    
        
Q3. Can we do modification in existing key value pair?

Q4. Write the code to change the class of given
dictionary to 'VII'
a = {'Class':'VI','Sec':'B','Rollno' : 1}

Q5. What do you mean by update() in context of
dictionary in python
   
Q6. What is the difference between del command and pop() 
     function in dictionaries?

Q7. Write the output of the following:
    a = {'Class':'VI','Sec':'B','Rollno' : 1}
    print('Class' in a)
    print('sec' in a)

Q8. Can there be any dictionary without key or
without value?if yes then give example.

Q9. Which function return the count of
    key-value pairs in the given dictionary.

Q10. Which function removes all the items from
    dictionary?


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

                              

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


Q1. Name a function which is used to create an empty dictionary.

Q2. What empty curly braces({}) shows in python?


Q3. Write the output of the following: 

        a = {}
        a[1]='A'
        a[2]='B'
        a[3]='C'
        print(a)

Q4. Write the output of the following:
        a = {'Class':'VI','Sec':'B','Rollno' : 1}
        print(a)
        print(a['Class'])
        print(a['Sec'])
        print(a['roll'])
if any of the above print statement return an error
than identify the error type.

Q5. a = {'Class':'VI','Sec':'B','Rollno' : 1}
    for i in a:
    print(i)

Q6. a = {'Class':'VI','Sec':'B','Rollno' : 1}
    for i in a:
    print(a)

Q7. a = {'Class':'VI','Sec':'B','Rollno' : 1}
    for i in a:
    print(a[i])

Q8. a = {'Class':'VI','Sec':'B','Rollno' : 1}
    for i in a:
      print(a[i])
    print(a[i])

Q9. What type of error shows by the following program:
    a = {'Class':'VI','Sec':'B','Rollno' : 1}
    for i in len(a):
    print(a[i])
Q10. What do you mean by traversing a dictionary?

Solution:

Q1. Name a function which is used to create an empty dictionary.

Ans. dict()

Q2. What empty curly braces({}) shows in python?

Ans. Curly braces shows empty dictionary in python.


Q3. Write the output of the following: 

        a = {}
        a[1]='A'
        a[2]='B'
        a[3]='C'
        print(a)
Ans. {1 : 'A', 2 : 'B', 3 : 'C'}

Q4. Write the output of the following:
        a = {'Class':'VI','Sec':'B','Rollno' : 1}
        print(a)
        print(a['Class'])
        print(a['Sec'])
        print(a['roll'])
if any of the above print statement return an error
than identify the error type.
Ans. {'Class': 'VI', 'Sec': 'B', 'Rollno': 1}      VI B
Last print statement will show "key error" as 'roll' key
is not there in dictionary

Q5. a = {'Class':'VI','Sec':'B','Rollno' : 1}
    for i in a:
    print(i)
Ans. Class
Sec
Rollno

Q6. a = {'Class':'VI','Sec':'B','Rollno' : 1}
    for i in a:
    print(a)
Ans. {'Class':'VI','Sec':'B','Rollno' : 1}
{'Class':'VI','Sec':'B','Rollno' : 1}
{'Class':'VI','Sec':'B','Rollno' : 1}

Q7. a = {'Class':'VI','Sec':'B','Rollno' : 1}
    for i in a:
    print(a[i])
Ans. VI
B
1

Q8. a = {'Class':'VI','Sec':'B','Rollno' : 1}
    for i in a:
      print(a[i])
    print(a[i])
Ans. VI
B
1
1

Q9. What type of error shows by the following program:
    a = {'Class':'VI','Sec':'B','Rollno' : 1}
    for i in len(a):
    print(a[i])
Ans. TypeError: 'int' object is not iterable
Q10. What do you mean by traversing a dictionary?
Ans. Traversing a dictionary means accessing each element of
dictionary. This can be done by using for loop.

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

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

Q1. Is dictionary in python is a collection or a sequence?

Q2. What do you mean by dictionary in python?

Q3 In dictionary we have _________ value concept.

Q4, In dictionary , key and it's value are separated by ________.

Q5. Entire dictionary is enclosed in _______ braces

Q6. Dictionary is _________(mutable/immutable)

Q7. The ______ of dictionary can be of any type but the _______ must be of immutable type.

Q8. Each key is associated with value.(T/F)

Q9. Write the code to create empty dictionary named 'd'

Q10. Write the output of the following:

            >>> a = {}

            >>>print(type(a))


Solution:

Q1. Is dictionary in python is a collection or a sequence?

Ans : Collection

Q2. What do you mean by dictionary in python?

Ans : Python dictionary is an unordered collection of items where each item is a KEY-VALUE pair

Q3 In dictionary we have _________ value concept.

Ans. Key

Q4, In dictionary , key and it's value are separated by ________.

Ans. colon(:)

Q5. Entire dictionary is enclosed in _______ braces

Ans. Curly -{ }

Q6. Dictionary is _________(mutable/immutable)

Ans. mutable

Q7. The ______ of dictionary can be of any type but the _______ must be of immutable type.

Ans 7- values, key

Q8. Each key is associated with value.(T/F)

Ans. True

Q9. Write the code to create empty dictionary named 'd'

Ans. d={ }

Q10. Write the output of the following:

            >>> a = {}

            >>>print(type(a))

Ans. <class 'dict'>

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