0% found this document useful (0 votes)
5 views9 pages

Program List 6

The document contains a series of programming exercises focused on creating and manipulating dictionaries in Python. Each question provides a specific task, such as creating a phone dictionary, counting keys with particular values, or checking for duplicate values, along with example inputs and expected outputs. The exercises aim to enhance understanding of dictionary operations and data handling in Python.

Uploaded by

aishwaryath903
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Program List 6

The document contains a series of programming exercises focused on creating and manipulating dictionaries in Python. Each question provides a specific task, such as creating a phone dictionary, counting keys with particular values, or checking for duplicate values, along with example inputs and expected outputs. The exercises aim to enhance understanding of dictionary operations and data handling in Python.

Uploaded by

aishwaryath903
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Computer Science File

Program List 6

Aishwarya Thakur

Q1) Write a program to create a phone dictionary for all your friends
and then print it.
Input:
phone = {}
n = int(input("How many friends: "))

for i in range(n):
name = input("Enter name: ")
num = input("Enter phone number: ")
phone[name] = num

print(phone)
Output:

Q2) Write a program that repeatedly asks the user to enter product names
and prices. Store all of these in a dictionary whose keys are the
product names and whose values are the prices.
Input:
products = {}

while True:
name = input("Enter product name (type stop to end): ")
if name == "stop":
break
price = int(input("Enter price: "))
products[name] = price

print(products)
Output:
Q3) A dictionary contains details of two workers with their names as
keys and other details in the form of a dictionary as value. Write a
program to print the worker information in records format. Example:
Worker={‘Amit’ : {‘age’:25, ‘sal’ : 12000}, ‘Diya’ : {‘age’:35 ,
‘sal’:13000}} Output: Employee Amit: Age:25 Salary:12000……
Input:
Worker = {
"Amit": {"age":25, "sal":12000},
"Diya": {"age":35, "sal":13000}
}

for name in Worker:


print("Employee", name)
print("Age:", Worker[name]["age"])
print("Salary:", Worker[name]["sal"])
Output:

Q4) Write a program to get dictionary keys as a list.


Input:
d = eval(input("Enter dictionary: "))
print(list([Link]()))
Output:

Q5) Write a program to count keys with particular values in the


dictionary.

Input:
data = {'Amit': 25, 'Diya': 30, 'Rohan': 25, 'Sita': 28, 'Karan': 25}
search_value = int(input("Enter value to count: "))
count = 0
for key in data:
if data[key] == search_value:
count += 1
print("Number of keys with value", search_value, "=", count)
Output:

Q6) Write a program to display minimum values keys in the dictionary.


Input:
data = {'Amit': 25, 'Diya': 30, 'Rohan': 22, 'Sita': 22, 'Karan': 28}
min_value = min([Link]())
print("Minimum value:", min_value)
for key, value in [Link]():
if value == min_value:
print(key)
Output:

Q7) 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}
Input:
n = int(input("Enter number of terms: "))
data = {}
for i in range(1, n+1):
data[i] = i*5
print("Dictionary:", data)
Output:

Q8) Write a program in python to remove the duplicate values from the
dictionary. Original dictionary={1: “Aman” , 2: “Suman” , 3: “Aman”} New
Dictionary ={1: “Aman” , 2: “Suman”}
Input:
d = {1: "Aman", 2: "Suman", 3: "Aman"}
new_dict = {}
for key in d:
if d[key] not in new_dict.values():
new_dict[key] = d[key]
print("New Dictionary =", new_dict)
Output:

Q9) WAP to check whether a given key already exists in a dictionary.


INPUT:{‘a’:100 , ‘b’ :200 ‘c’:300} Key:b Output:Present,value:200 Key:w
Output:Not Present.
Input:
data = {'a': 100, 'b': 200, 'c': 300}
key = input("Enter key to search: ")
if key in data:
print("Present, value:", data[key])
else:
print("Not Present")
Output:

Q10) WAP to check for None values in given dictionary.(HINT:use in


operator) The original dictionary is :{ ‘Maths’:35, ‘CS’:35 , ‘ENG’:35 ,
‘PHYSICS’:35 , ‘CHEMISTRY’:35 , ‘PE’:None} Does Dictionary contain None
Value? True (otherwise False).
Input:
data={'Maths':35,'CS':35,'ENG':35,'PHYSICS':35,'CHEMISTRY':35,'PE':None}
if None in [Link]():
print("Does Dictionary contain None Value? True")
else:
print("Does Dictionary contain None Value? False")
Output:

Q11) .WAP to check if tuple exists as dictionary keys.(HINT: check type


in dictionary) The original dictionary is :{ (3,4) : ‘hello’ , (7,8) :
‘best’ ,6:’is’) Does tuple exists as dictionary key? True (otherwise
False)
Input:
data={(3,4):'hello',(7,8):'best',6:'is'}
found=False
for k in data:
if type(k)==tuple:
found=True
break
print("Does tuple exists as dictionary key?",found)
Output:

Q12) Write a Python program to input names of ‘n’ employees and their
salary details like basic salary, house rent and conveyance allowance.
Calculate the total salary of each employee and display. Enter the
number of entries: 3 Enter the name of the employee: Siya Enter the
basic salary: 10000 Enter house rent allowance:3000 Enter conveyance
allowance:500 Name Net Salary Siya 13500​
Input:
n=int(input("Enter the number of entries: "))
emp={}
for i in range(n):
name=input("Enter the name of the employee: ")
basic=int(input("Enter the basic salary: "))
hra=int(input("Enter house rent allowance: "))
conv=int(input("Enter conveyance allowance: "))
emp[name]=basic+hra+conv
print("Name","Net Salary")
for k,v in [Link]():
print(k,v)
Output:
Q13) WAP to store students' names and their percentage in a dictionary,
delete a particular student name from the dictionary. Also display the
dictionary after deletion.
Input:
n=int(input("Enter number of students: "))
d={}
for i in range(n):
name=input("Enter name: ")
per=float(input("Enter percentage: "))
d[name]=per
del_name=input("Enter student name to delete: ")
if del_name in d:
del d[del_name]
print("Dictionary after deletion:",d)
Output:

Q14) Write a program that checks if two same values in a dictionary have
different keys. For dictionary D1={‘a’ :10 , ‘b’:20 , ‘c’:10} , the
program should print “ 2 keys have same values” and for dictionary D2={
‘a’:10 ,’b’:20, ‘c’:30}, the program should print “No keys have same
values”.
Input:
d={'a':10,'b':20,'c':10}
vals=list([Link]())
if len(vals)!=len(set(vals)):
print("2 keys have same values")
else:
print("No keys have same values")
Output:

Q15) Create a dictionary whose keys are month names and whose values are
the number of days in the corresponding months. • Ask the user to enter
a month name and use the dictionary to tell how many days are in the
month. • Print out all of the keys in alphabetical order. • Print out
all of the months with 31 days • Print out the (key-value)pairs sorted
by the number of days in each month.
Input:
months={'January':31,'February':28,'March':31,'April':30,'May':31,'June':3
0,'July':31,'August':31,'September':30,'October':31,'November':30,'Decembe
r':31}
m=input("Enter month name: ")
if m in months:
print("Days:",months[m])
print("Months in alphabetical order:")
for k in sorted(months):
print(k)
print("Months with 31 days:")
for k in months:
if months[k]==31:
print(k)
print("Months sorted by number of days:")
items=list([Link]())
for i in range(len(items)):
for j in range(i+1,len(items)):
if items[i][1]>items[j][1]:
items[i],items[j]=items[j],items[i]
for k,v in items:
print(k,v)
Output:
Q16) Write a Python program to input names of ‘n’ countries and
currency,store it in a dictionary. Also search and display for a
particular country.
Input:
n=int(input("Enter number of countries: "))
d={}
for i in range(n):
c=input("Enter country name: ")
cur=input("Enter currency: ")
d[c]=cur
s=input("Enter country to search: ")
if s in d:
print("Currency:",d[s])
else:
print("Country not found")
Output:
Q17) Given a list containing these values
[22,13,28,13,22,25,7,13,25].Write a code to calculate the mean , median
and mode of this list.
Input:
import statistics
data=[22,13,28,13,22,25,7,13,25]
print("Mean:",[Link](data))
print("Median:",[Link](data))
print("Mode:",[Link](data))
Output:

Q18) Write a code to generate two random integers between 450 and 950
.Print these numbers along their average.
Input:
import random as r
a=[Link](450,950)
b=[Link](450,950)
print(a,b)
print(a+b/2)
Output:

You might also like