Skip to content

Commit cba1cce

Browse files
committed
uploading assignment
1 parent 73597d7 commit cba1cce

File tree

1 file changed

+291
-0
lines changed

1 file changed

+291
-0
lines changed

Dictionary/Assignment/Solutions.md

+291
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
2+
### **Access, Change, Add, and Remove Items**
3+
4+
1. **Employee Database**: You have an employee database stored in a dictionary where employee IDs are the keys and their names are the values. Write a Python program to:
5+
- Access and print the name of an employee by their ID.
6+
- Update the name of an employee by their ID.
7+
- Add a new employee to the dictionary.
8+
- Remove an employee from the dictionary after they leave the company.
9+
10+
```python
11+
employees_db = {
12+
"101": "Rajesh Kumar",
13+
"102": "Sita Patel",
14+
"103": "Amit Sharma",
15+
"104": "Priya Nair",
16+
"105": "Vikram Singh",
17+
"106": "Neha Gupta",
18+
"107": "Arjun Mehta",
19+
"108": "Anjali Rao"
20+
}
21+
22+
# access item
23+
print("Access item")
24+
print("-"*32)
25+
26+
employee_id = input("Enter id : ")
27+
if employee_id in employees_db:
28+
print(f"id: {employee_id} , name = {employees_db[employee_id]}")
29+
else:
30+
print("invalid id")
31+
32+
print("-"*32)
33+
34+
# update item
35+
print("Update item")
36+
print("-"*32)
37+
38+
employee_id = input("Enter id : ")
39+
if employee_id in employees_db:
40+
print(f"id: {employee_id} , name = {employees_db[employee_id]}")
41+
new_name = input("Enter new name : ")
42+
employees_db[employee_id] = new_name
43+
print("Record upated successfully.")
44+
print(f"id: {employee_id} , name = {employees_db[employee_id]}")
45+
else:
46+
print("invalid id")
47+
48+
print("-"*32)
49+
50+
# add item
51+
print("Add item")
52+
print("-"*32)
53+
54+
employee_id = input("Enter your id : ")
55+
employee_name = input("Enter your name : ")
56+
57+
employees_db.update({employee_id:employee_name})
58+
print("New record added successfully.")
59+
print(f"id: {employee_id} , name = {employee_name}")
60+
61+
print("-"*32)
62+
63+
64+
# Remove item
65+
print("Remove item")
66+
print("-"*32)
67+
68+
employee_id = input("Enter id : ")
69+
if employee_id in employees_db:
70+
employee_name = employees_db[employee_id]
71+
print(f"id: {employee_id} , name = {employee_name}")
72+
employees_db.pop(employee_id)
73+
print("Data deleted successfully.")
74+
else:
75+
print("invalid id")
76+
77+
print("-"*32)
78+
79+
```
80+
81+
2. **Shopping Cart**: You are building a shopping cart system where items and their prices are stored in a dictionary. Write a program to:
82+
- Access and print the price of a specific item.
83+
- Add a new item to the shopping cart with its price.
84+
- Update the price of an existing item.
85+
- Remove an item from the shopping cart once the user removes it.
86+
```python
87+
items_prices = {
88+
"apple": 30,
89+
"banana": 10,
90+
"mango": 50,
91+
"orange": 20,
92+
"grapes": 40,
93+
}
94+
95+
# access item
96+
print("Access item")
97+
print("-"*32)
98+
99+
item_name = input("Enter item name : ")
100+
if item_name in items_prices:
101+
print(f"name : {item_name} , price = ${items_prices[item_name]}")
102+
else:
103+
print("invalid item name")
104+
105+
print("-"*32)
106+
107+
```
108+
109+
110+
3. **Online Courses**: You manage a dictionary where course names are the keys and the number of enrolled students is the value. Write a Python program to:
111+
- Access the number of students enrolled in a specific course.
112+
- Add a new course and its student count.
113+
- Update the number of students for a specific course.
114+
- Remove a course that has been discontinued.
115+
116+
4. **Bookstore Inventory**: You are maintaining a bookstore inventory where each book title is a key, and its quantity in stock is the value. Write a Python program to:
117+
- Access the quantity of a specific book.
118+
- Add a new book to the inventory.
119+
- Update the stock quantity for an existing book.
120+
- Remove a book once it is out of stock.
121+
122+
5. **Voting System**: You are managing a voting system where candidates’ names are the keys, and the number of votes they’ve received is the value. Write a Python program to:
123+
- Access the current votes for a specific candidate.
124+
- Add a new candidate to the voting system.
125+
- Update the votes for a candidate when they receive more.
126+
- Remove a candidate from the system if they drop out of the race.
127+
128+
---
129+
130+
### **Looping Over Dictionaries**
131+
132+
6. **Student Marks**: You have a dictionary where student names are the keys, and their marks are the values. Write a Python program to loop through the dictionary and:
133+
- Print each student's name and their marks.
134+
- Calculate and print the average marks of all students.
135+
```python
136+
marks_data = {
137+
"John": 70,
138+
"Rahul": 100,
139+
"Manoj": 50,
140+
"Raj": 80,
141+
"Aman": 40,
142+
}
143+
total_marks = 0
144+
145+
for name,marks in marks_data.items():
146+
print(f"{name} : {marks} marks")
147+
total_marks += marks
148+
149+
avg_marks = total_marks / len(marks_data)
150+
print(f"Average marks = {avg_marks}")
151+
```
152+
153+
7. **City Temperatures**: You are tracking the temperatures of various cities using a dictionary where the city name is the key and the temperature is the value. Write a Python program to:
154+
- Print each city’s name and its temperature.
155+
- Loop through the dictionary to find the city with the highest temperature.
156+
157+
8. **Company Salaries**: You have a dictionary that stores employee names as keys and their salaries as values. Write a Python program to:
158+
- Print each employee’s name and salary.
159+
- Calculate the total payroll (sum of all salaries).
160+
- Find and print the employee with the highest salary.
161+
```python
162+
salary_data = {
163+
"John": 7000,
164+
"Rahul": 10000,
165+
"Manoj": 500000,
166+
"Raj": 80000,
167+
"Aman": 40000,
168+
}
169+
total_payroll = 0
170+
highest_salary = ["",0]
171+
for name,salary in salary_data.items():
172+
print(f"{name} : {salary} marks")
173+
# calculat total payroll
174+
total_payroll += salary
175+
# calculating highest salary
176+
if salary > highest_salary[1]:
177+
highest_salary[0] = name
178+
highest_salary[1] = salary
179+
180+
print(f"Total payroll = {total_payroll}")
181+
print(f"Highest salary {highest_salary[0]} with {highest_salary[1]}")
182+
```
183+
184+
9. **Library Books**: A library tracks the number of times each book has been borrowed in a dictionary. Write a Python program to:
185+
- Print each book title and the number of times it has been borrowed.
186+
- Loop through the dictionary to find the book that has been borrowed the most.
187+
188+
10. **E-Commerce Sales**: You are maintaining a dictionary where product names are keys, and the number of units sold is the value. Write a Python program to:
189+
- Print each product and its sales count.
190+
- Calculate and print the total number of units sold for all products.
191+
192+
```python
193+
product_data = {
194+
"milk": 50,
195+
"bread": 10,
196+
"cookies": 30,
197+
"chocobar": 90,
198+
"gram": 40,
199+
}
200+
total_sale = 0
201+
202+
for product_name,sale in product_data.items():
203+
print(f"{product_name} : {sale} ")
204+
total_sale += sale
205+
206+
print(f"Total sale = {total_sale}")
207+
```
208+
209+
---
210+
211+
### **Copying Dictionaries**
212+
213+
11. **School Subjects**: You have a dictionary where subjects are the keys, and the number of students enrolled in each subject is the value. Write a program to:
214+
- Create a copy of the dictionary.
215+
- Make changes to the original dictionary (e.g., add students to a subject).
216+
- Demonstrate that changes in the original do not affect the copied version.
217+
218+
12. **Car Inventory**: A car dealership has an inventory stored in a dictionary where car models are the keys, and the number of available units is the value. Write a program to:
219+
- Create a copy of the inventory.
220+
- Add new models to the original inventory.
221+
- Print both the original and copied inventories to show that they are independent.
222+
223+
13. **Financial Data**: You maintain a dictionary of monthly expenses where months are the keys, and the amount spent is the value. Write a program to:
224+
- Copy the dictionary.
225+
- Modify the original dictionary (e.g., add new expenses for the current month).
226+
- Verify that the copied dictionary remains unchanged.
227+
228+
14. **Movie Ratings**: You have a dictionary where movie names are the keys, and user ratings are the values. Write a Python program to:
229+
- Create a copy of the dictionary.
230+
- Modify the original dictionary by adding new movies or updating ratings.
231+
- Show how the copied dictionary is not affected by changes to the original.
232+
233+
15. **Restaurant Menu**: You manage a restaurant menu in a dictionary where dish names are keys, and their prices are the values. Write a program to:
234+
- Copy the menu.
235+
- Change prices in the original menu.
236+
- Show how the copy remains unaffected by price updates in the original.
237+
238+
---
239+
240+
### **Nested Dictionaries**
241+
242+
16. **University Courses**: You are managing a university system where departments are keys, and their values are dictionaries containing courses as keys and student enrollments as values. Write a Python program to:
243+
- Access the number of students enrolled in a specific course within a specific department.
244+
- Add a new course to a department.
245+
- Update the enrollment number for an existing course.
246+
247+
17. **Company Hierarchy**: Create a nested dictionary where department names are keys, and the values are dictionaries where employee names are keys, and their roles are values. Write a Python program to:
248+
- Access the role of a specific employee in a specific department.
249+
- Add a new employee to a department.
250+
- Update the role of an employee.
251+
252+
18. **Family Tree**: Create a nested dictionary to represent a family tree where each family member’s name is the key, and the value is another dictionary containing information such as age, relation, and occupation. Write a program to:
253+
- Access and print the occupation of a specific family member.
254+
- Update the occupation or age of a family member.
255+
- Add a new member to the family tree.
256+
257+
19. **Classroom Management**: Create a nested dictionary where each class is the key, and the value is another dictionary with student names as keys and their grades as values. Write a Python program to:
258+
- Access a student's grade in a specific class.
259+
- Add a new student and their grade to a class.
260+
- Update the grade of an existing student.
261+
262+
20. **Online Orders**: Create a nested dictionary to represent customer orders where customer names are the keys, and the values are dictionaries containing products as keys and the quantities ordered as values. Write a Python program to:
263+
- Access the quantity of a specific product ordered by a specific customer.
264+
- Add a new product to an existing customer's order.
265+
- Update the quantity of a product in an order.
266+
267+
Here are **5 questions** related to **dictionary methods** with real-world examples or scenarios to complete the list:
268+
269+
### **Dictionary Methods**
270+
271+
21. **Currency Converter**: You are building a currency converter where the keys are currency codes (e.g., "USD", "EUR"), and the values are exchange rates to the local currency. Write a Python program to:
272+
- Use the `get()` method to retrieve the exchange rate for a given currency. If the currency is not found, return a default message like "Currency not available."
273+
274+
22. **Employee Records**: You maintain a dictionary of employee IDs as keys and employee names as values. Write a Python program to:
275+
- Use the `pop()` method to remove an employee from the dictionary by their ID and print the updated dictionary. If the employee ID is not found, handle it gracefully.
276+
277+
23. **Student Grades**: You are managing student grades where student names are the keys, and their grades are the values. Write a Python program to:
278+
- Use the `items()` method to print each student's name along with their grade.
279+
- Use the `keys()` and `values()` methods to print all student names and their respective grades separately.
280+
281+
24. **Grocery Prices**: You have a dictionary that stores grocery items as keys and their prices as values. Write a Python program to:
282+
- Use the `update()` method to modify the price of an item, and add a new item if it doesn't already exist.
283+
- Use the `setdefault()` method to add an item to the dictionary only if it doesn't exist, and print the updated dictionary.
284+
285+
25. **Website Traffic Data**: You are tracking website visits where the keys are URLs and the values are the number of visits. Write a Python program to:
286+
- Use the `popitem()` method to remove and return the last added (URL, visits) pair from the dictionary.
287+
- Use the `clear()` method to reset the visit count by clearing the entire dictionary and print the empty dictionary.
288+
289+
290+
291+
---

0 commit comments

Comments
 (0)