Page 233 - Information_Practice_Fliipbook_Class11
P. 233
values = list(studentScores.values())
values.sort(reverse=True)
if len(values) >= 2:
highestTwo = values[:2]
else:
highestTwo = values
print("Original Dictionary:", studentScores)
print("Highest Two Values:", highestTwo)
9. Write a Python program to create a dictionary from a string 'w3resource' such that each individual character mates a key and its index
value for fist occurrence males the corresponding value in dictionary.
Expected output : {'3': 1, 's': 4, 'r': 2, 'u': 6, 'w': 0, 'c': 8, 'e': 3, 'o': 5}
Ans. myStr = 'w3resource'
myDict = {}
index = 0
for char in myStr:
if char not in myDict:
myDict[char] = index
index +=1
print("Dictionary :", myDict)
10. Write a program to input your friend’s, 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 for 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
Ans. friendsDict = {}
while True:
print("\nMenu:")
print("1. Display Friends")
print("2. Add Friend")
print("3. Delete Friend")
print("4. Modify Phone")
print("5. Check Friend")
print("6. Display Sorted Dictionary")
print("7. Exit")
choice = int(input("Enter your choice (1-7): "))
if choice == 1:
print("Friends and Phone Numbers:")
for name, phone in friendsDict.items():
print(f"{name}: {phone}")
elif choice == 2:
name = input("Enter friend's name: ")
Python Dictionaries 219

