Professional Documents
Culture Documents
Question 1:
Write programs to sort the dictionary {'ravi': '10', 'rajnish': '9', 'sanjeev': '15',
'yash': '2', 'suraj': '32'} and print
ANSWER:
dict = {'ravi': '10', 'rajnish': '9', 'sanjeev': '15', 'yash':
'2', 'suraj': '32'}
OUTPUT:
('rajnish', '9')
('ravi', '10')
('sanjeev', '15')
('suraj', '32')
('yash', '2')
[('rajnish', '9'), ('ravi', '10'), ('sanjeev', '15'), ('suraj', '32'), ('yash', '2')]
SBOA SCHOOL AND JUNIOR COLLEGE, CHENNAI – 101
#Defining a dictionary
dic =
{"A":12,"B":13,"C":9,"D":89,"E":34,"F":17,"G":65,"H":36,"I":25,"J
":11}
OUTPUT:
Highest value: 89
Second highest value: 65
Question 3
dic = {}
SBOA SCHOOL AND JUNIOR COLLEGE, CHENNAI – 101
for ch in st:
if ch in dic:
#if next character is already in the dictionary
dic[ch] += 1
else:
#if ch appears for the first time
dic[ch] = 1
OUTPUT:
Enter a string: meritnation
{'m': 1, 'e': 1, 'r': 1, 'i': 2, 't': 2, 'n': 2, 'a': 1, 'o': 1}
Question 4:
write a program to enter names of employees and their salaries as input
and store them in a dictionary. Here n is to input by the user.
ANSWER:
#Program to create a dictionary which stores names of employees
#and their salary
num = int(input("Enter the number of employees whose data to be
stored: "))
count = 1
# Create an empty dictionary
employee = dict()
# Entering data
for count in range (num):
name = input("Enter the name of the Employee: ")
salary = int(input("Enter the salary: "))
employee[name] = salary
# Displaying data
SBOA SCHOOL AND JUNIOR COLLEGE, CHENNAI – 101
OUTPUT:
EMPLOYEE_NAME SALARY
Gomathi 55000
Rohith 65000
Suraj 65000
Moveen 65000
Anusri 65000
Question 5:
Write a program to count the number of times a character appears in a given string
ANSWER:
#Count the number of times a character appears in a given string
st = input("Enter a string: ")
dic = {} #creates an empty dictionary
for ch in st:
if ch in dic: #if next character is already in dic
dic[ch] += 1
else:
dic[ch] = 1 #if ch appears for the first time
# Display keys
SBOA SCHOOL AND JUNIOR COLLEGE, CHENNAI – 101
OUTPUT:
Enter a string: SBOAJC SCHOOL
S:2
B:1
O:3
A:1
J:1
C:2
H: 1
L:1
Question 6:
Write a program to convert a number entered by the user into its corresponding
number in words. for example if the input is 876 then the output should be ‘Eight
Seven Six’.
ANSWER:
#number is stored as string
num = input("Enter any number: ")
#numberNames is a dictionary of digits and corresponding number
#names
numberNames = {0:'Zero',1:'One',2:'Two',3:'Three',4:'Four',\
5:'Five',6:'Six',7:'Seven',8:'Eight',9:'Nine'}
result = ''
for ch in num:
key = int(ch) #converts character to integer
value = numberNames[key]
result = result + ' ' + value
print(str)
Question 7:
Repeatedly ask the user to enter a team name and how many games the team has
won and how many they lost. Store this information in a dictionary where the keys
are the team names and the values are lists of the form [wins, losses].
(a) Using the dictionary created above, allow the user to enter a team name and
print out the team's winning percentage.
(b) Using the dictionary, create a list whose entries are the number of wins of each
team.
(c) Using the dictionary, create a list of all those teams that have winning records.
ANSWER:
d = {}
ans = "y"
while ans == "y" or ans == "Y" :
name = input("Enter Team name: ")
SBOA SCHOOL AND JUNIOR COLLEGE, CHENNAI – 101
w_team = []
for i in d.values():
w_team.append(i[0])
w_rec = []
for i in d:
if d[i][0] > 0:
w_rec.append(i)
Output
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.
When the user is done entering products and prices, allow them to repeatedly enter
a product name and print the corresponding price or a message if the product is not
in the dictionary.
ANSWER
d = {}
ans = "y"
while ans == "y" or ans == "Y" :
p_name = input("Enter the product name: ")
p_price = float(input("Enter product price: "))
d[p_name] = p_price
ans = input("Do you want to enter more product names? (y/n): ")
ans = "y"
while ans == "y" or ans == "Y" :
p_name = input("Enter the product name to search: ")
print("Price:", d.get(p_name, "Product not found"))
ans = input("Do you want to know price of more products? (y/n):
")
Output
Create a dictionary whose keys are month names and whose values are the number of days in
the corresponding months.
(a) Ask the user to enter a month name and use the dictionary to tell how many days are in the
month.
(d) Print out the (key-value) pairs sorted by the number of days in each month.
ANSWER
days_in_months = {
"january":31,
"february":28,
"march":31,
"april":30,
"may":31,
"june":30,
"july":31,
"august":31,
"september":30,
"october":31,
"november":30,
"december":31
}
if m not in days_in_months:
print("Please enter the correct month")
else:
print("There are", days_in_months[m], "days in", m)
day_month_lst = []
for i in days_in_months:
day_month_lst.append([days_in_months[i], i])
day_month_lst.sort()
SBOA SCHOOL AND JUNIOR COLLEGE, CHENNAI – 101
sorted_days_in_months = dict(month_day_lst)
print()
print("Months sorted by days:", sorted_days_in_months)
Output
Question 10
Can you store the details of 10 students in a dictionary at the same time ? Details
include - rollno, name, marks, grade etc. Give example to support your answer.
ANSWER
n = 10
details = {}
for i in range(n):
name = input("Enter the name of student: ")
roll_num = int(input("Enter the roll number of student: "))
marks = int(input("Enter the marks of student: "))
grade = input("Enter the grade of student: ")
details[roll_num] = [name, marks, grade]
print()
print(details)
Output
ANSWER
Output
Question 12
Given two dictionaries say D1 and D2. Write a program that lists the overlapping keys of
the two dictionaries, i.e., if a key of D1 is also a key of D2, then list it.
ANSWER
Output
Question 13
Write a program that checks if two same values in a dictionary have different keys.
That is, 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.
ANSWER
val = tuple(D1.values())
seen = []
flag = True
for i in val:
if i not in seen:
seen.append(i)
count = val.count(i)
if count > 1:
print(count, "keys have same value of", i)
flag = False
if flag:
print("No keys have same values")
Output
Enter a dictionary D1: {'a': 10, 'b': 20, 'c': 10, 'd': 40, 'e':
10, 'f': 20}
D1 = {'a': 10, 'b': 20, 'c': 10, 'd': 40, 'e': 10, 'f': 20}
3 keys have same value of 10
2 keys have same value of 20
Question 14
d1 = {1:11, 2:12}
d2 = {1:11, 2:12, 3:13, 4:15}
Output
Enter a dictionary d1: {1:11, 2:12}
Enter a dictionary d2: {1:11, 2:12, 3:13, 4:15}
d1 = {1: 11, 2: 12}
d2 = {1: 11, 2: 12, 3: 13, 4: 15}
{1: 11, 2: 12} is contained in {1: 11, 2: 12, 3: 13, 4: 15}
Question 15
A dictionary D1 has values in the form of lists of numbers. Write a program to
create a new dictionary D2 having same keys as D1 but values as the sum of the
list elements e.g.,
D1 = {'A' : [1, 2, 3] , 'B' : [4, 5, 6]}
then
D2 is {'A' :6, 'B' : 15}
ANSWER
Output
Enter a dictionary D1: {'A' : [1, 2, 3] , 'B' : [4, 5, 6]}
D1 = {'A': [1, 2, 3], 'B': [4, 5, 6]}
{'A': 6, 'B': 15}
Question 16:
Write a program to input your friends’ names and their Phone Numbers and store them
in the dictionary as the key-value pair. Perform the following operations on the
dictionary:
a) Display the name and phone number of all your friends
b) Add a new key-value pair in this dictionary and display the modified dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
f) Display the dictionary in sorted order of names
ANSWER:
dic = {}
#Creates an empty dictionary
#Adding a contact
if(inp == 1):
name = input("Enter your friend name: ")
phonenumber = input("Enter your friend's contact number:
")
dic[name] = phonenumber
SBOA SCHOOL AND JUNIOR COLLEGE, CHENNAI – 101
OUTPUT:
1. Add New Contact
2. Modify Phone Number of Contact
3. Delete a Friend's contact
4. Display all entries
5. Check if a friend is present or not
6. Display in sorted order of names
7. Exit
Enter your choice(1-7): 1
Enter your friend name: Mohit
Enter your friend's contact number: 98765*****
Contact Added