1) a = {1:2 ,2:1 ,4:3 ,3:4 ,6:5 ,5:6 }
#this will print a sorted list of the keys
print(sorted(a.values()))
#this will print the sorted list with items.
print(sorted(a.items()))
2) d = {0:10, 1:20}
print(d)
d.update({2:30})
print(d)
3) name,temp = input("enter city name and temperature and enter 0 0 for discontinuing").split()
t = {}
t.update({name:int(temp)})
while name and int(temp):
name,temp = input("enter city name and temperature").split()
t.update({name:int(temp)})
name = input("enter city name")
print(t[name])
4) test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33]
print("The original list : " + str(test_list))
key_list = ["name", "number"]
n = len(test_list)
res = []
for idx in range(0, n, 2):
res.append({key_list[0]: test_list[idx], key_list[1] : test_list[idx + 1]})
print("The constructed dictionary list : " + str(res))
5) vin = ['John','Smith','Alice','Daniel']
kp = [14, 13, 32, 21]
Dict = dict(zip(vin, kp))
print(Dict)
vin = ['John','Smith','Alice','Daniel']
kp = [14, 13, 32, 21]
inp= input("Enter your choice : print or add or remove or query: ")
if inp == 'print':
Dict = dict(zip(vin, kp))
for a in Dict:
print(a,"==>",Dict[f"{a}"])
elif inp =='add':
emp= input("Enter the name of the Employee to be added: ")
if emp.title() in Dict.keys():
print(f"{emp} name is already existing!")
else:
salr = input(f"Enter the salary of the {emp} ")
Dict.update({emp : salr})
print(Dict)
elif inp == 'remove':
name = input("Enter the name of the employee to be removed? ")
if name in Dict:
Dict.pop(name)
print(Dict)
else:
print(f"Sorry!!, {name} doesn't exist in the data!")
elif inp == 'query':
emp= input("Enter the name of the employee you want to query: ").title()
if emp in Dict.keys():
print(f"The salary of {emp} is {Dict[emp]}")
else:
print(f"The employee named {emp} doesn't exist!!")
else:
print("Enter a valid choice")
QUESTIONS ON SETS:-
1) Frozen set is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. Due to this, frozen sets can be used as keys in Dictionary or as elements of another set.
setname = (1, 2, 3, 4, 5, 6, 7, 8, 9)
fnum = frozenset(setname)
print("frozenset Object is : ", fnum)
2) set1 = {10,20,30,40,50}
set2 = {40,50,60,70,80}
print("Original sets:")
print(set1)
print(set2)
print("Difference of set1 and set2 using difference():")
print(set1.difference(set2))