ASSIGNMENT - 3, GO_STP_8113
GO_STP_8113
ASSIGNMENT - 3
Goeduhub Technologies
Task link : https://www.goeduhub.com/11531/dictionary-question-exercise-practice-solutions-learning
#OnlineSummerTraining #machinelearning #datascience #PYTHONNUMPY #pandas #pythonprogramming #goeduhub
SOLUTIONS:
Q.1 Write a Python Program to sort (ascending and descending) a dictionary by value.
Q. 4 Write a Python program to convert list to list of dictionaries.
Sample lists: ["Black", "Red", "Maroon", "Yellow"], ["#000000", "#FF0000", "#800000", "#FFFF00"]
Expected Output: [{'color_name': 'Black', 'color_code': '#000000'}, {'color_name': 'Red', 'color_code': '#FF0000'}, {'color_name': 'Maroon', 'color_code': '#800000'}, {'color_name': 'Yellow', 'color_code': '#FFFF00'}]
list1=["Black", "Red", "Maroon", "Yellow"]
list2=["#000000", "#FF0000", "#800000", "#FFFF00"]
dic = [{'color_name': c, 'color_name':n} for c,n in zip(list1,list2)]
print(dic)
Q. 5 We have following information on Employees and their Salary (Salary is in lakhs),
| Employee | Salary |
| John | 14 |
| Smith | 13 |
| Alice | 32 |
| Daneil | 21 |
- Using above create a dictionary of Employees and their Salary
dicti = {'John':14, 'Smith':13, 'Alice':32, 'Daniel':32}
print('Data Of Employees And Their Salary',dicti)
- Write a program that asks user for three type of inputs,
- print: if user enter print then it should print all Employees with their Salary in this format,
- John ==>14
- Smith ==>13
- Alice ==>32
- Daneil ==>21
- add: if user input adds then it should further ask for an Employee name to add. If Employee already exists in our dataset then it should print that it exists and do nothing. If it doesn't then it asks for Salary and add that new Employee/Salary in our dictionary and print it
- remove: when user inputs remove it should ask for an Employee to remove. If an Employee exists in our dictionary then remove it and print a new dictionary using format shown above in (a). Else print that Employee doesn't exist!
- query: on this again ask the user for which Employee he or she wants to query. When a user inputs that Employee it will print the Salary of that Employee.
Questions on Sets-
Q.1 What is the difference between a set and a frozenset? Create any set and try to use frozenset(setname).
The frozenset() function returns an unchangeable frozenset object.
Sets are used to store multiple items in a single variable which is unordered and unindexed.
setname= ["apple", "banana", "cherry"]
print(frozenset(setname))
Q.2 Find the elements in a given set that are not in another set
set1 = {10,20,30,40,50}
set2 = {40,50,60,70,80}
Difference between set1 and set2 is {10,20,30}
set1 = {10,20,30,40,50}
set2 = {40,50,60,70,80}
print(set1.difference(set2))
Comments
Post a Comment