Python Dictionary Assignment and Solutions

Python Dictionary Exercise, Assignment, and Solutions are based on Python Dictionary data type. These questions will test your understanding of the Python dictionary. This Python Dictionary Assignment will check your understanding of Python keys and values and how to Python Dictionary behave with keys and values. How the data is processed using keys and values and how the dictionary behaves with comparison operation.

Q1. Is Python dictionary is a sequential Data structure in Python?

Q2. Which is mutable in Python Dictionary keys or values?

Q3. What will be the output of the following code
D1 = {‘john’:40,’peter’:45}
D2 = {‘john’:440,’peter’:245}
>>>D1==D2

Q4. What will be the output of the following code
D1 = {‘john’:40,’peter’:45}
D2 = {‘john’:440,’peter’:245}
>>>D1 > D2

solution

The above code will generate an erro message as dictionary does not support > (greater than) < (less than ) operators 

Q5. Suppose a dictionary d= {‘john’:40,’peter’:45}. Which command is suitable to find out the length of this dictionary.
a) d.size() b) len(d) c) size(d) d) d.len( )

Q6. What will be the output of the following
D= {‘john’:40,’peter’:45}
>> ‘john’ in D

Q7. Create a Python Dictionary of student having the following values
Admission No : 1234 Student Name : rakesh kumar stream : science fees : 2345.56 grade : A

Q8. Consider a Python dictionary Emp as per the following declarations
Emp ={ ‘empcode’: 1102 , ‘Name’: ‘arjun singh’ ,’ dept’: ‘sales’ ,’salary : 35000, ‘desig’:’manager’}
Change the name of employee of the above dictionary to ‘ ram singh dinkar’

Q9. Add a new key in the above employee dictionary ‘date_join’ with value ’16-11-2020’

Q10. Remove a key ‘desig’ from the above dictionary of employee.

Q11. Consider a dictionary of Parts
Parts ={ ‘partno’ 1234, ‘partName’: ‘screw driver’, ‘price’: 56.50, ‘supplier’:’ ABN Amro’}
Find out whether the supplier key exists in the above dictionary exists or not using membership operator
solution
Parts ={ ‘partno’ 1234, ‘partName’: ‘screw driver’, ‘price’: 56.50, ‘supplier’:’ ABN Amro’} if ‘supplier’ in Parts: print(‘Supplier key exists’) else: print(‘ supplier key does not exists’)

Q12. Display the contents of the following dictionary using Python For Loop
Parts ={ ‘partno’ 1234, ‘partName’: ‘screw driver’, ‘price’: 56.50, ‘supplier’:’ ABN Amro’}
solutions

Parts ={ ‘partno’ 1234, ‘partName’: ‘screw driver’, ‘price’: 56.50, ‘supplier’:’ ABN Amro’}
for x in Parts:
    print('Key:',x, 'value :',Parts[x])

2nd solution:
for value in Parts.values():
   print( values)

3nd solution:
for key,value in Parts.items():
    print(key, value)

Q13. Write a Python program to read the admission number and names of 10 students from the keyboard. Create a dictionary of these admission number and names and then display them on the screen
solution

dict1={}
for x in range(10):
    admno = input('Enter admission no :')
    name = input('Enter name :')
    dict1['admno'] = admno 
    dict1['name'] = name

print(dict1) 

Q14. Write a program in Python to find out the frequency of each number in stored in a list using a python dictionary.
Example
List1 = [1,2,3,4,5,6,7,8,9,7,6,2,4,2,5,23,6,4]
Output ={1: 1, 2:2,3:1,4:4,5:1,6:2,7:7,23:1}
solution

List1 = [1,2,3,4,5,6,7,8,9,7,6,2,4,2,5,23,6,4]
dict1={}
for x in list1:
    if x not in dict1:
       dict1[x] = list1.count(x)
print(dict1)

Q15. Write a program in Python to read the admission number, name of student and his/her stream of 10 students and create a dictionary from this information. Find out whether the name ‘Arjun’ exist in this list or not. If yes then display his/her complete information on the screen otherwise display “Name does not exist’.
solution:

dict1={}
for x in range(10):
     admno = input('Enter admission no:')
     name = input('Enter admission no:')
     stream = input('Enter admission no:')
     dict1['admno']= admno
     dict1['name']= name
     dict1['stream' ='stream
print(dict1)
for x in dict1:
   if dict[x] ='Arjun':
      print(' Arjun exists in this list'
   else:
      print('arjun does not exist' 
       

Q16. Create a Dictionary of numbers that contains the number and its square as its value.
Example
Numbers ={ 1: 1, 2: 4, 3:9, 4:16, 5:25, 6:36, 7:49,8:64,9:81,10:100}

solution:

dict1={}
for x in range(1,11):
    dict1[x]=x**2
print(dict1)
If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.