Programs on Dictionary in Python Class 11 | Important Questions-Solved Exercise

Share with others

Dictionary in Python
Dictionary in Python

Programs on Dictionary in Python Class 11

Q1. Write a Python program to arrange the values in a dictionary in ascending order. For example

Original Dictionary = {1 : 25, 2 : 21, 3 : 23}

Expected Output = [21, 25, 32]

Solution:

d={1:21,2:32,3:25}

print(sorted(d.values()))

print(d)

Programs on Dictionary in Python Class 11

Q2. Write a python programs to join/merge/concatenate the following two dictionaries and create the new dictionary.

d1 = {1: “Amit”, 2 : “Suman”}

d2 = {4 : “Ravi”, 5 : “Kamal”}

New dictionary = {1: “Amit”, 2 : “Suman”, 4 : “Ravi”, 5 : “Kamal”}

Solution:

d1 = {1 :”Amit”,2 : “Suman”}

d2 = {4:”Ravi”, 5 : “Kamal”}

d3={ }

for i in (d1,d2):

  d3.update(i)

print(d3)

Programs on Dictionary in Python Class 11

Q3. Write a function check(key) which takes a key as an argument and check whether that key is present in dictionary or not.

Solution:

d1 = {1 :”Amit”,2 : “Suman”}

def check(i):

  for k in d1:

    if k==i:

      print(“key is present”)

      break

  else:

      print(“key is not present”)

check(1) #This function is only to verify code

Programs on Dictionary in Python Class 11

Q4.Accept the number of terms say n from the user and display the dictionary in the form of {n: n*5} for example:

If number of terms entered by user is 4 then the expected dictionary is {1:5, 2:10, 3: 15, 4: 20}

Solution:

d1 = { }

n=int(input(“Enter any number”))

for i in range(n):

  d1[i+1]=(i+1)*5

print(d1)

Programs on Dictionary in Python Class 11

Q5. Write a program to add the values of given dictionary.

d1 = {1:2, 2: 90, 3: 50}

Solution :

d1 = {1:2, 2:90, 3: 50}

print(sum(d1.values()))

Programs on Dictionary in Python Class 11

Q6. Write a program to add the keys of given dictionary.

d1 = {1:2, 2: 90, 3: 50}

Solution :

d1 = {1:2, 2:90, 3: 50}

print(sum(d1.keys()))

Programs on Dictionary in Python Class 11

Q7. Write a program to multiply all the values of given dictionary.

d1 = {1:2, 2: 90, 3: 50}

Solution :

d1 = {1:2,2:90, 3 : 50}

s=1

for i in d1:

  s=s*i

print(s)

Programs on Dictionary in Python Class 11

Q8. Write a program to accept a key from the user and remove that key from the dictionary if present.

Solution :

d1 = {1 : 2, 2 : 90, 3 : 50}

k=int(input(“Enter any key”))

if k in d1:

  d1.pop(k)

  print(d1)

else:

  print(“Key not found”)

Programs on Dictionary in Python Class 11

Q9. Write a program in python to display the maximum and minimum value in dictionary.

Solution:

d1 = {1 : 21, 2 : 90, 3 : 50}

mx=max(d1.values())

mn=min(d1.values())

print(“Maximum Value is “ ,mx)

print(“Minimum Value is “ ,mn)

Q10. Write a program in python to remove the duplicate values from the dictionary. For example:

Original dictionary = {1 : “Aman”, 2: “Suman”, 3: “Aman”}

New dictionary = {1 : “Aman”, 2: “Suman”}

Solution:

d1={1:”Aman”,2:”Suman”,3:”Aman”}

nd1={ }

for k,v in d1.items():

  if v not in nd1.values():

    nd1[k]=v

print(nd1)

Q11. Write a program to count the number of elements in a dictionary.

Solution:

d1={1:”Aman”,2:”Suman”,3:”Aman”,4:”Kranti”,5:”Suman”}

print(len(d1))

Q12. Accept a key from the user and modify it’s value.

Solution:

d1={1:”Aman”,2:”Suman”,3:”Aman”,4:”Kranti”,5:”Suman”}

k=int(input(“Enter the key”))

v=input(“Enter the modified value”)

d1[k]=v

print(d1)

Q13. Write a program to store information of products like product id, product name, and product price in a dictionary by taking product id as a key.

Solution:

t=int(input(“Enter number of terms”))

prod={ }

for i in range(t):

  pid=int(input(“Enter product id”))

  pn=input(“Enter product name”)

  pp=int(input(“Enter product price”))

  temp=(pn, pp)

  prod[pid]=temp

print(prod)

Q14. Write a program to accept the employee id from the user and display its detail from the dictionary. Data is stored in the dictionary in the following format

{Empid : (Empname, EmpSalary}

Solution:

emp={1:(“Amit”,25000),2:(“Suman”,30000),3:(“Ravi”,36000)}

pid=int(input(“Enter the product id”))

l=[ ]

if pid in emp:

  l=emp[pid]

  print(“Employee id, Employee Name”, “\t””Salary”)

  print(pid,”\t””\t””\t””\t”,l[0],”\t””\t”,l[1])

else:

  print(“Record not found”)

Q14. Write a program to accept the employee id from the user and check Salary. If salary is less than 25000 then increase the salary by Rs1000. Data is stored in the dictionary in the following format

{Empid : (Empname, EmpSalary}

Solution:

emp={1:(“Amit”,25000),2:(“Suman”,30000),3:(“Ravi”,36000)}

pid=int(input(“Enter the product id”))

l=[ ]

if pid in emp:

  l=emp[pid]

  if l[1]>25000:

    emp[pid]=(l[0],l[1]+500)

print(emp)

Q15. Write a program to display the name of all the employees whose salary is more than 25000 from the following dictionary.

emp={1:(“Amit”,25000),2:(“Suman”,30000),3:(“Ravi”,36000)}

Format of data is given below :

{Empid : (Empname, EmpSalary}

Solution:

emp={1:(“Amit”,25000),2:(“Suman”,30000),3:(“Ravi”,36000)}

d=list(emp.values())

print(“Employees whose Salary more than 25000 are”)

for i in d:

if i[1]>25000:

print(i[0])

Q16. Write a program to count the frequency of each word in a given string accepted from the user using dictionary.

Solution:

str1=input(“Enter any String”)

w=str1.split()

d={ }

for i in w:

if i not in d:

d[i]=w.count(i)

for i in d:

print(“frequency of “, i , ” is ” , d[i])

Q17. Write a program to accept roll number, names and marks of five students and store the detail in dictionary using roll number as key. Also display the sum of marks of all the five students.

Solution:

d={ }

s=0

for i in range(5):

rn = int(input(“Enter roll number”))

nm = input(“Enter name “)

mrk = input(“Enter marks”)

temp=(nm, mrk)

d[rn]=temp

for i in d:

L=d[i]

s=s+int(L[1])

print(“Sum of marks “, s)

Q18. Write a program to count the frequency of each character in a given string accepted from the user using dictionary.(Store the character as key and it’s frequency as value)

Solution:

str1=input(“Enter any String”)

d={ }

for i in str1:

if i not in d:

d[i]=str1.count(i)

for i in d:

print(“frequency of “, i , ” is ” , d[i])


Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

CBSE Class 12 Board Exam Date Sheet 2020-21


Share with others

Leave a Reply

%d bloggers like this: