Ass5 Itw
Ass5 Itw
Roll No:232051
Batch:B2
Aim: Write a program to demonstrate use of Dictionaries in python and perform: Accessing values in
dictionaries, Working with dictionaries- Create, Merge, Delete set of keys,
Rename
#creating the dictionary
dict={1:"java",2:"python",3:"javascript"}
print(dict)
#creating dictionary tarnsersing dfunction
print ("value ofthe key are:")
for i in dict:
print(dict[i])
#create
print("creating the empty dictionary ")
d={}
d["key1"]="networking"
d["key2"]="probability"
d["key3"]="data structure"
d["key4"]="itw"
for i in d:
print(d[i])
print("merging of the dictionary")
dict.update(d)
print(dict)
d.update(dict)
print(d)
print("Keya are:",d.keys())
print("values are: ",d.values())
print(d.get("key1"))
#pop the values
d.pop("key1")
print(d)
d.clear()
print(d)
# write the program to add the keys in dictionary
newdict={**dict,**{8:"Shivaji"}}
print(newdict)
#sort the dictionary by value
values=list(dict.values())
sorted_values=sorted(values)
print(sorted_values)
sortedlist=sorted(dict.items(),key=lambda x:x[1],reverse=True)
print(sortedlist)
#concatinate
dic1={1:10,2:20}
dic2={3:30,4:40}
dic4={5:50,6:60}
newdic={**dic1,**dic2,**dic4}
print(newdic)
OUTPUT:
{1: 'java', 2: 'python', 3: 'javascript'}
value ofthe key are:
java
python
javascript
creating the empty dictionary
networking
probability
data structure
itw
merging of the dictionary
{1: 'java', 2: 'python', 3: 'javascript', 'key1': 'networking', 'key2': 'probability', 'key3': 'data structure',
'key4': 'itw'}
{'key1': 'networking', 'key2': 'probability', 'key3': 'data structure', 'key4': 'itw', 1: 'java', 2: 'python', 3:
'javascript'}
Keya are: dict_keys(['key1', 'key2', 'key3', 'key4', 1, 2, 3])
values are: dict_values(['networking', 'probability', 'data structure', 'itw', 'java', 'python', 'javascript'])
networking
{'key2': 'probability', 'key3': 'data structure', 'key4': 'itw', 1: 'java', 2: 'python', 3: 'javascript'}
{}
{1: 'java', 2: 'python', 3: 'javascript', 'key1': 'networking', 'key2': 'probability', 'key3': 'data structure',
'key4': 'itw', 8: 'Shivaji'}
['data structure', 'itw', 'java', 'javascript', 'networking', 'probability', 'python']
[(2, 'python'), ('key2', 'probability'), ('key1', 'networking'), (3, 'javascript'), (1, 'java'), ('key4', 'itw'),
('key3', 'data structure')]
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}