Program to Execute in Lab
Program to Execute in Lab
2. Write a program to sum all the values of a dictionary. Hint dict1 = {‘key 1’: 200,
‘key 2’: 300} Result: 500
dict1 = {'key 1': 200, 'key 2': 300}
total_sum = sum(dict1.values())
print("Result:", total_sum)
11. Write a program to demonstrate the concept of pretty printing using dictionaries
import pprint
message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
count = { }
for char in message:
count.setdefault(char,0)
count[char] = count[char]+1
pprint.pprint(count)
12. program to create a dictionary conisisting of state & their corresponding dishes.
The program should accept a state name from a user and generate the
corresponding dishes for Eg- Punjab-->Aloo Paratha, Pujabi Lassi Karnataka
--> Jowar Roti, Bisi belebath maharastra--> Pav bhaji, vada pav gujarat-->
Dhokla, Khandvi
st_dish = { }
flag = False
ans = 'y'
while ans == 'y':
s = input("Enter the state:")
n = int(input("Enter the Number of dishes:"))
dishes = list()
print("Enter the dishes:")
for i in range(0,n):
dishes.append(input())
st_dish[s] = dishes
ans = input("please enter q to quit and y to continue")
print("State", "\t", "Dishes")
for s, dish in st_dish.items():
print(s, "\t",dish)
state = input("Enter the state whose dishes you want:")
for s, dish in st_dish.items():
if s == state:
flag= True
print(s,"\t",dish)
if flag == False:
print("The state ",state,"is not there:")
13. Program to accept a sentence and generate the frequency of words using
dictionaries
sent = input("Enter a sentence:").lower()
word = sent.split()
freq = {}
for w in word:
freq[w] = freq.get(w,0) + 1
k = freq.keys()
print("Frequency List")
print("--------------------")
for key in k:
print("%s: %d" %(key,freq[key]))