|
| 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 | +2. **Shopping Cart**: You are building a shopping cart system where items and their prices are stored in a dictionary. Write a program to: |
| 11 | + - Access and print the price of a specific item. |
| 12 | + - Add a new item to the shopping cart with its price. |
| 13 | + - Update the price of an existing item. |
| 14 | + - Remove an item from the shopping cart once the user removes it. |
| 15 | + |
| 16 | +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: |
| 17 | + - Access the number of students enrolled in a specific course. |
| 18 | + - Add a new course and its student count. |
| 19 | + - Update the number of students for a specific course. |
| 20 | + - Remove a course that has been discontinued. |
| 21 | + |
| 22 | +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: |
| 23 | + - Access the quantity of a specific book. |
| 24 | + - Add a new book to the inventory. |
| 25 | + - Update the stock quantity for an existing book. |
| 26 | + - Remove a book once it is out of stock. |
| 27 | + |
| 28 | +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: |
| 29 | + - Access the current votes for a specific candidate. |
| 30 | + - Add a new candidate to the voting system. |
| 31 | + - Update the votes for a candidate when they receive more. |
| 32 | + - Remove a candidate from the system if they drop out of the race. |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +### **Looping Over Dictionaries** |
| 37 | + |
| 38 | +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: |
| 39 | + - Print each student's name and their marks. |
| 40 | + - Calculate and print the average marks of all students. |
| 41 | + |
| 42 | +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: |
| 43 | + - Print each city’s name and its temperature. |
| 44 | + - Loop through the dictionary to find the city with the highest temperature. |
| 45 | + |
| 46 | +8. **Company Salaries**: You have a dictionary that stores employee names as keys and their salaries as values. Write a Python program to: |
| 47 | + - Print each employee’s name and salary. |
| 48 | + - Calculate the total payroll (sum of all salaries). |
| 49 | + - Find and print the employee with the highest salary. |
| 50 | + |
| 51 | +9. **Library Books**: A library tracks the number of times each book has been borrowed in a dictionary. Write a Python program to: |
| 52 | + - Print each book title and the number of times it has been borrowed. |
| 53 | + - Loop through the dictionary to find the book that has been borrowed the most. |
| 54 | + |
| 55 | +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: |
| 56 | + - Print each product and its sales count. |
| 57 | + - Calculate and print the total number of units sold for all products. |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +### **Copying Dictionaries** |
| 62 | + |
| 63 | +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: |
| 64 | + - Create a copy of the dictionary. |
| 65 | + - Make changes to the original dictionary (e.g., add students to a subject). |
| 66 | + - Demonstrate that changes in the original do not affect the copied version. |
| 67 | + |
| 68 | +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: |
| 69 | + - Create a copy of the inventory. |
| 70 | + - Add new models to the original inventory. |
| 71 | + - Print both the original and copied inventories to show that they are independent. |
| 72 | + |
| 73 | +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: |
| 74 | + - Copy the dictionary. |
| 75 | + - Modify the original dictionary (e.g., add new expenses for the current month). |
| 76 | + - Verify that the copied dictionary remains unchanged. |
| 77 | + |
| 78 | +14. **Movie Ratings**: You have a dictionary where movie names are the keys, and user ratings are the values. Write a Python program to: |
| 79 | + - Create a copy of the dictionary. |
| 80 | + - Modify the original dictionary by adding new movies or updating ratings. |
| 81 | + - Show how the copied dictionary is not affected by changes to the original. |
| 82 | + |
| 83 | +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: |
| 84 | + - Copy the menu. |
| 85 | + - Change prices in the original menu. |
| 86 | + - Show how the copy remains unaffected by price updates in the original. |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +### **Nested Dictionaries** |
| 91 | + |
| 92 | +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: |
| 93 | + - Access the number of students enrolled in a specific course within a specific department. |
| 94 | + - Add a new course to a department. |
| 95 | + - Update the enrollment number for an existing course. |
| 96 | + |
| 97 | +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: |
| 98 | + - Access the role of a specific employee in a specific department. |
| 99 | + - Add a new employee to a department. |
| 100 | + - Update the role of an employee. |
| 101 | + |
| 102 | +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: |
| 103 | + - Access and print the occupation of a specific family member. |
| 104 | + - Update the occupation or age of a family member. |
| 105 | + - Add a new member to the family tree. |
| 106 | + |
| 107 | +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: |
| 108 | + - Access a student's grade in a specific class. |
| 109 | + - Add a new student and their grade to a class. |
| 110 | + - Update the grade of an existing student. |
| 111 | + |
| 112 | +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: |
| 113 | + - Access the quantity of a specific product ordered by a specific customer. |
| 114 | + - Add a new product to an existing customer's order. |
| 115 | + - Update the quantity of a product in an order. |
| 116 | + |
| 117 | +Here are **5 questions** related to **dictionary methods** with real-world examples or scenarios to complete the list: |
| 118 | + |
| 119 | +### **Dictionary Methods** |
| 120 | + |
| 121 | +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: |
| 122 | + - 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." |
| 123 | + |
| 124 | +22. **Employee Records**: You maintain a dictionary of employee IDs as keys and employee names as values. Write a Python program to: |
| 125 | + - 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. |
| 126 | + |
| 127 | +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: |
| 128 | + - Use the `items()` method to print each student's name along with their grade. |
| 129 | + - Use the `keys()` and `values()` methods to print all student names and their respective grades separately. |
| 130 | + |
| 131 | +24. **Grocery Prices**: You have a dictionary that stores grocery items as keys and their prices as values. Write a Python program to: |
| 132 | + - Use the `update()` method to modify the price of an item, and add a new item if it doesn't already exist. |
| 133 | + - Use the `setdefault()` method to add an item to the dictionary only if it doesn't exist, and print the updated dictionary. |
| 134 | + |
| 135 | +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: |
| 136 | + - Use the `popitem()` method to remove and return the last added (URL, visits) pair from the dictionary. |
| 137 | + - Use the `clear()` method to reset the visit count by clearing the entire dictionary and print the empty dictionary. |
| 138 | + |
| 139 | +--- |
0 commit comments