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.

import operator
x = {'A':51,'B':5,'C':7,'D':89,'E':2}
ascd = sorted(x.items(), key=operator.itemgetter(1))
print('Ascending Order is : ',ascd)
desc = sorted(x.items(), key=operator.itemgetter(1), reverse=True)
print('Descending Order is : ',desc)


Q.2 Write a Python Program to add a key to a dictionary. 

a = {'A':7,'B':84,'C':9,'D':5}
a.update({'E':8})
print(a)


3 Write a  program asks for City name and Temperature and builds a dictionary using that Later on you can input City name and it will tell you the Temperature of that City.

dic=dict()
n = int(input("Enter no. of cities"))
for i in range(1,n+1):
    x=input("Enter city name")
    y=int(input("Enter temperature of city"))
    dic[x]=y
print(dic)
z=input("Enter name of city to check temperature")
print("Temperature of ",x,"is",dic[x])


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),

EmployeeSalary
John14
Smith13
Alice32
Daneil21
  1. 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)

  1. Write a program that asks user for three type of inputs,
    1. print: if user enter print then it should print all Employees with their Salary in this format,
      1. John ==>14
      2. Smith ==>13
      3. Alice ==>32
      4.  Daneil ==>21
    2. 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
    3. 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!
    4. 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.

dicti = {'John':14, 'Smith':13, 'Alice':32, 'Daniel':32}
n = input("Write print/add/remove/query")
if n=='print':
    for i in dicti.items():
        print(i[0],'==>',i[1])
elif n=='add':
    a=input("Enter employee name")
    if a==dicti.keys():
        print("Already exists")
    else:
        b=input("Enter employee salary")
        dicti[a]=b
        for j in dicti.items():
            print(j[0],'==>',j[1])
elif n=='remove':
    c=input("Enter employee name to remove")
    if c in dicti.keys():
        dicti.pop(c)
        for k in dicti.items():
            print(k[0],'==>',k[1])
    else:
        print("Employee dosen't exist")
elif n=='query':
    d=input("Enter employee name to query")
    if d in dicti.keys():
        print(dicti[d])
else:
    print("Invalid input")
        

 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

Popular posts from this blog

Data Science Matplotlib Library Data Visualization, ASSIGNMENT - 6, GO_STP_8113