Ad Code

Responsive Advertisement

Python Dictionary : Exercise, Practice, Solutions and Problems

Here you will get the Python Dictionary Problems and Solutions for Python Dictionary Operation, python Dictionary vs list and many more.


Question 1-6 contains text based Questions from 7-8 contains Find the error type Questions from 9-12 contains Find the Output type Questions and questions  13-14 contain Write a program type Question. And two Questions at the Last Contain Question for your Practice.


1. What is a Dictionary in Python?


Solution-:   A Dictionary in Python refers to a mapping of unique keys to value which is a collection of Key-value pair.

2. Dictionaries in Python are Mutable or Immutable?

Solution-:    Dictionaries are Mutable which means their values cannot be changed once assigned.


3. What are the difference between lists and Dictionaries?

Solution-: 

In list -: 

  • Elements are enclosed in Square Brackets.
  • Index value Should be an Integer.

In Dictionaries-:

  • Elements are enclosed in curly brackets.
  • Index Value can be of any Data Type which is key in Python Dictionaries. 

4. What is a Key-Value Pair in Python Dictionary?

Solution-:  Python Dictionary Key refers to the pair of indices and a set of value, where Key represents indices and Value represents the value assigned to the indices.


5. How to create an Empty Python Dictionary?

Solution-:  To create a Empty Python Dictionary we use Curly braces having zero characters.

dict = {}

6. Find the Error in the Given Code?


dict1 = dict[ ]
i=1
n = input ( "Enter the Number of Entries : ")
while i<=n:
   a = input ("Enter the name : ")
   b = input ("Enter the age : ")
   d1(a) = b
   i = i+1
l = d1.key[]
for i in l :
   print ( i,'/t','d1[i]')


Solution-: 

dict1 = dict()
i=1
n = int(input( "Enter the Number of Entries : "))
while i<=n:
   a = input ("Enter the name : ")
   b = input ("Enter the age : ")
   d1[a] = b
   i = i+1
l = d1.key()
for i in l :
   print ( i,'/t',d1[i])


7. Find the Error in the Program.

d1 = ( 1:100,5:500 )
print{ d1.keys()}


Solution-:
d1 = ( 1:100,5:500 )
print{ d1.keys()}

8. Find the error in the program.

d1 = ( 1:100; 2:200; 3:3000)
print( d1.keys[])

Solution-: 

d1 = ( 1:1000, 2:2000, 3:3000 )
print (d1.keys() )

9. Find The Output-:


d1 = { 1:100, 2:200, 3:300, 4:400, 5:500 }
print(d1.items())
print(d1.keys())
print(d1.values())

Solution-:

dict_items([(1, 100), (2, 200), (3, 300), (4, 400), (5, 500)])
dict_keys([1, 2, 3, 4, 5])
dict_values([100, 200, 300, 400, 500])


10. Find The Output-:


d1 = { 1:100, 2:200, 3:300, 4:400, 5:500 }
d2 = { 6:600, 7:700, 8:800 }
d1.update(d2)
print(d1)

Solution-:  {1: 100, 2: 200, 3: 300, 4: 400, 5: 500, 6: 600, 7: 700, 8: 800}


11. Find the Output-:


d1 = { 1:100, 2:200, 3:300, 4:400, 5:500, 6:600, 7:700, 8:800 }
del d1[3]
print(d1)

Solution-: {1: 100, 2: 200, 4: 400, 5: 500, 6: 600, 7: 700, 8: 800}


12. Find the Output-:


d1 = { 1:100, 2:200, 3:300, 4:400, 5:500, 6:600, 7:700, 8:800 }
d1.pop(5)
print(d1)

Solution-: {1: 100, 2: 200, 3: 300, 4: 400, 6: 600, 7: 700, 8: 800}


13. Write a Program to input n names and Phone Numbers to store it in a dictionary and print the phone number of a particular name.


Solution-:

phone = dict()
n=int ( input ("Enter the Number of Entries : "))
for i in range(0,n,1):
   a = input("Enter the Name : ")
   b =int( input("Enter the Phone Number : "))
   phone[a]=b
l = phone.keys()
x = input("Enter the Name : ")
for i in l :
   if x == i:
      print(x," :  phone Number is : ",phone[i])
   else:
      print("Does Not Exists")

Output-:

Enter the Number of Entries : 2
Enter the Name : Piyush
Enter the Phone Number : 6652779935
Enter the Name : Sam
Enter the Phone Number : 8865142537

Enter the Name : Piyush
Piyush  :  phone Number is :  6652779935


14. Write a Python program to input 'n' employees and their salary like Basic salary, House rent and conveyance allowance. Calculate Total salary of each employee and Display.


Solution-:

d1 = dict()
n=int ( input ("Enter the Number of Entries : "))
for i in range(0,n,1):
   a = input("Enter the Name : ")
   b =int( input("Enter the Basic Salary : "))
   c =int( input("Enter the Home Rent  Allowance : "))
   d =int( input("Enter Conveyance Allowance : "))
   d1[a] = [ b, c, d ]
l = d1.keys()
print("Name ",'\t\t',"Salary")
for i in l :
   salary = 0
   z = d1[i]
   for r in z :
      salary=salary+r
   print(i , '\t\t',salary)   

Output-:

Enter the Number of Entries : 2
Enter the Name : Piyush
Enter the Basic Salary : 100
Enter the Home Rent  Allowance : 200
Enter Conveyance Allowance : 300
Enter the Name : Sam
Enter the Basic Salary : 400
Enter the Home Rent  Allowance : 500
Enter Conveyance Allowance : 0
Name  Salary
Piyush 600
Sam      900

Question for Practice?


1.Write a Program to Update the elements in a Dictionary?

2. Write a program to store the Student Name , Roll Number , Phone Number and Email Id in a Python Dictionary and print the detail of a particular Student?

__________________________

Comment Below for Query




-

Post a Comment

0 Comments