PYTHON DICTIONARY
PRACTICAL QUESTIONS
IP - GRADE XI
Q1. WAP to create a dictionary containing names of competition winner students as KEY and number of their wins as values.
n=int(input("How Many Students ?"))
compwinner={ }
for i in range(n):
key=input("Enter Name of the Student:")
value=int(input("Enter No of Wins:"))
compwinner[key]=value
print("The Dictionary is:", compwinner)
Q2.WAP TO INPUT TOTAL NO OF SECTIONS AND STREAM IN 11TH CLASS AND DISPLAY ALL.
cxi={}
n=int(input("Enter n no of sections:"))
i=1
while i<=n:
sec=input("Enter section:")
stre=input("Enter Stream:")
cxi[sec]=stre
i+=1
print("class section stream")
for i in cxi:
print("XI", i , cxi[i])
Q3. WAP to create a dictionary containing roll number of students and percentage as
values for n number of students display the total number of students scoring more than 80
stud={ }# Empty dictionary created
n=int(input("enter mumber of students - "))
for i in range(n):
rno = input("enter Roll No - ")
stud[rno] = float(input("Enter percentage –"))
ctr=0
for rno in stud:
if stud[rno]>80:
ctr+=1
print("No. of students scoring percentage above 80 –" ,ctr)
Q4. Program to create a dictionary which stores names(key) of employees and their salary(value)
num = int(input("Enter the number of employees whose data to be stored: "))
count = 1
employee = {}
for count in range (num):
name = input("Enter the name of the Employee: ")
salary = int(input("Enter the salary: "))
employee[name] = salary
print("\n\nEMPLOYEE_NAME \tSALARY")
for k in employee:
print(k,'\t\t',employee[k])
Q5.Dictionary key pointing to different values
dic = {}
while True :
nam = input("Enter name :-")
phone = float(input("Enter phone number :-"))
cost = float(input("Enter cost :-"))
item = input("Enter item :-")
dic[ nam ] = [ phone , item , cost ]
a = input("Do you want to enter more records enter (Yes/ No) :- ")
if a == "No" or a == "no":
break
for i in dic :
print()
print("Name : ", i )
print("Phone : ", dic[ i ][ 0 ] , "\t"
, "Cost : ", dic[ i ][ 1 ] , "\t",
"Item : ", dic[ i ][ 2 ])
Q6.Display product if present in the dictionary otherwise invalid message
dic = { }
while True :
product = input("enter the name of product (enter q for quit )= ")
if product == "q" or product == "Q" :
break
else :
price = int(input("enter the price of product = "))
dic [ product ] = price
while True :
name = input("enter the name of product those you are
entered (enter q for quit )= ")
if name == "q" or name == "Q" :
break
else :
if name not in dic :
print("name of product is invaild")
else :
print("price of product = ",dic[name])
Q6. Write a Python program that accepts a value and checks whether the inputted value is part of given dictionary or not. If it is present, check for the frequency of its occurrence and print the corresponding key otherwise print an error message.
'''
dic = eval(input("Enter a dictionary :- "))
val = eval(input("Enter a value :-"))
lst = list( dic.values() )
if val in lst :
print("Frequency of occurrence of ",val ," is ", lst.count( val ))
print("Its key are :-" )
for i in dic :
if dic [i] == val :
print(i)
else :
print("Not found !!")
Comments
Post a Comment