0% found this document useful (0 votes)
4 views7 pages

Ass 1

The document contains a series of Python programming exercises focused on dictionary operations, including creating, modifying, and displaying dictionaries. It covers tasks such as storing friends' names and phone numbers, finding the highest values in a dictionary, creating a dictionary from a string, and converting numbers to words. Each exercise includes input examples, code snippets, and expected output.

Uploaded by

parasharyash513
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)
4 views7 pages

Ass 1

The document contains a series of Python programming exercises focused on dictionary operations, including creating, modifying, and displaying dictionaries. It covers tasks such as storing friends' names and phone numbers, finding the highest values in a dictionary, creating a dictionary from a string, and converting numbers to words. Each exercise includes input examples, code snippets, and expected output.

Uploaded by

parasharyash513
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
You are on page 1/ 7

COMPUTER SCIENCE

DICTIONARY
YASH PARASHAR XI-G

Question:1
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:
• Display the name and phone number for all your friends.
• Add a new key-value pair in this dictionary and display the modified
dictionary.
• Enter the name whose phone number you want to modify. Display the
modified dictionary.
• Enter the friend’s name and check if a friend is present in the dictionary or
not.
• Display the dictionary in sorted order of names.

Input:
friends = {}
# Input friends' names and phone numbers
n = int(input("Enter the number of friends: "))
for _ in range(n):
name = input("Enter friend's name: ")
phone = input("Enter phone number: ")
friends[name] = phone
# Display all friends
print("Friends List:")
for name, phone in friends.items():
print(name, ":", phone)
# Add a new friend
new_name = input("Enter new friend's name: ")
new_phone = input("Enter new friend's phone number: ")
friends[new_name] = new_phone
print("Modified Dictionary:")
print(friends)
# Modify an existing friend's phone number
modify_name = input("Enter the name whose phone number you want to modify: ")
if modify_name in friends:
new_phone = input("Enter new phone number: ")
friends[modify_name] = new_phone
print("Modified Dictionary:")
print(friends)
else:
print("Friend not found!")
# Check if a friend is in the dictionary
search_name = input("Enter the friend's name to search: ")
if search_name in friends:
print(search_name, "is in the dictionary with phone number",
friends[search_name])
else:
print("Friend not found!")
# Display dictionary in sorted order of names
sorted_friends = dict(sorted(friends.items()))
print("Dictionary in sorted order:")
for name, phone in sorted_friends.items():
print(name, ":", phone)

Output:
Enter the number of friends: 2
Enter friend's name: Yash
Enter phone number: 9869696989
Enter friend's name: Uddu
Enter phone number: 1234567891
Friends List:
Yash : 9869696989
Uddu : 1234567891
Enter new friend's name: Veer
Enter new friend's phone number: 12345183413
Modified Dictionary:
{'Yash': '9869696989', 'Uddu': '1234567891', 'Veer': '12345183413'}
Enter the name whose phone number you want to modify: Yash
Enter new phone number: 98712144890
Modified Dictionary:
{'Yash': '98712144890', 'Uddu': '1234567891', 'Veer': '12345183413'}
Enter the friend's name to search: Uddu
Uddu is in the dictionary with phone number 1234567891
Dictionary in sorted order:
Uddu : 1234567891
Veer : 12345183413
Yash : 98712144890

Question:2
Write a Python program to find the highest 2 values in a dictionary.
Input:
#Dictionary
data = {"A": 45, "B": 78, "C": 89, "D": 23, "E": 67}
# Find the highest two values
sorted_values = sorted(data.values(), reverse=True)
highest_two = sorted_values[:2]
print("Highest two values:", highest_two)

Output:
Highest two values: [89, 78]
Question:3
Write a Python program to create a dictionary from a string.
Input:
s = input("Enter a string: ")
char_dict = {}
for char in s:
if char in char_dict:
char_dict[char] += 1
else:
char_dict[char] = 1
print(char_dict)

Output:
Enter a string: I Love You
{'I': 1, ' ': 2, 'L': 1, 'o': 2, 'v': 1, 'e': 1, 'Y': 1, 'u': 1}

Question:4
Create a dictionary ‘ODD’ of odd numbers between 1 to 10, where the
key is the number and the value is the corresponding number in words.
• Display the keys in the Dictionary ODD
• Display the values in the Dictionary ODD
• Display the items in the Dictionary ODD
• Find the length of the Dictionary ODD
• Check if 7 is present or not in the Dictionary ODD
• Check if 2 is present or not in the Dictionary ODD
• Retrieve the value corresponding to the key 9
• Delete the item from the Dictionary ODD, corresponding to the
key 9
Input:
# Create the dictionary ODD
ODD = {1: "one", 3: "three", 5: "five", 7: "seven", 9: "nine"}
# Display the keys in the Dictionary ODD
print("Keys in ODD:", ODD.keys())
# Display the values in the Dictionary ODD
print("Values in ODD:", ODD.values())
# Display the items in the Dictionary ODD
print("Items in ODD:", ODD.items())
# Find the length of the Dictionary ODD
print("Length of ODD:", len(ODD))
# Check if 7 is present or not in the Dictionary ODD
if 7 in ODD:
print("7 is present in the dictionary.")
else:
print("7 is not present in the dictionary.")
# Check if 2 is present or not in the Dictionary ODD
if 2 in ODD:
print("2 is present in the dictionary.")
else:
print("2 is not present in the dictionary.")
# Retrieve the value corresponding to the key 9
print("Value corresponding to key 9:", ODD[9])
# Delete the item from the Dictionary ODD, corresponding to the key 9
del ODD[9]
print("Updated ODD after deletion:", ODD)

Output:
Keys in ODD: dict_keys([1, 3, 5, 7, 9])
Values in ODD: dict_values(['one', 'three', 'five', 'seven', 'nine'])
Items in ODD: dict_items([(1, 'one'), (3, 'three'), (5, 'five'), (7, 'seven'), (9, 'nine')])
Length of ODD: 5
7 is present in the dictionary.
2 is not present in the dictionary.
Value corresponding to key 9: nine
Updated ODD after deletion: {1: 'one', 3: 'three', 5: 'five', 7: 'seven'}
Question:5
Write a program to convert a number entered by the user into its
corresponding number in words. For example, if the number is 876, then
the output should be ‘Eight Seven Six’.
Input:
num = input("Enter a number: ")
# Dictionary mapping digits to words
digit_to_word = {"0": "Zero", "1": "One", "2": "Two", "3": "Three", "4": "Four", "5":
"Five", "6": "Six", "7": "Seven", "8": "Eight", "9": "Nine"}
# Convert the number into words
words = [digit_to_word[digit] for digit in num]
# Join the words with a space and display the result
print("The number in words is:", " ".join(words))

Output:
Enter a number: 100
The number in words is: One Zero Zero

Question:6
Write a program to create a dictionary which stores names of employees
and their salary.
Input:
# Create an empty dictionary
employee_salary = {}
# Number of employees to add
n = int(input("Enter the number of employees: "))
# Loop to input employee names and salaries
for i in range(n):
name = input("Enter the name of employee: ")
salary = float(input("Enter the salary of " + name + ": "))
employee_salary[name] = salary
# Display the dictionary
print("Employee Salary Dictionary:")
print(employee_salary)

Output:
Enter the number of employees: 2
Enter the name of employee: Uddu
Enter the salary of Uddu: 200000
Enter the name of employee: Veer
Enter the salary of Veer: 100000
Employee Salary Dictionary:
{'Uddu': 200000.0, 'Veer': 100000.0}

You might also like