Page 454 - Computer Science V2.0 Class 11
P. 454
'Emma': 88
}
highestTwoValues = findHighestTwoValues(studentScores)
print("Original Dictionary:", studentScores)
print("Highest Two Values:", highestTwoValues)
main()
4. Write a Python program to create a dictionary from a string.
Note: Track the count of the letters from the string.
Sample string : 'w3resource'
Expected output : {'3': 1, 's': 1, 'r': 2, 'u': 1, 'w': 1, 'c': 1, 'e': 2, 'o': 1}
Ans. myStr = 'w3resource'
myDict = {}
index = 0
for char in myStr:
if char not in myDict:
myDict[char] = index
index +=1
print("Dictionary :", myDict)
5. 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
Ans. a. def displayFriends(myDict):
print("Friends and Phone Numbers:")
for name, phone in myDict.items():
print(f"{name}: {phone}")
def addFriend(myDict, name, phone):
myDict[name] = phone
print(f"\n'{name}' added. Updated dictionary:")
displayFriends(myDict)
def deleteFriend(myDict, name):
if name in myDict:
del myDict[name]
print(f"\n'{name}' deleted. Updated dictionary:")
displayFriends(myDict)
else:
print(f"\n'{name}' not found in the dictionary.")
def modifyPhone(myDict, name, newPhone):
if name in myDict:
myDict[name] = newPhone
440 Touchpad Computer Science (Ver. 2.0)-XI

