-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_10dictionaries.py
More file actions
102 lines (73 loc) · 2.2 KB
/
Copy pathday_10dictionaries.py
File metadata and controls
102 lines (73 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#-------------------------------------------------------------------------------------------------------------
#day10 Dictionaries
#-------------------------------------------------------------------------------------------------------------
#1. Create and Access
student={
"name":"Ram",
"age": 19,
"city": "Chennai"
}
print(student["name"])
print(student["age"])
print(student["city"])
#---------------------------------------------------------------------------------------------------------------
#2.add and update
student={
"name":"Sammy",
"age": 49,
}
#adding new key
student["city"]="Hyderabad"
print("After adding a new key:", student)
#updating existing value
student["age"]= 20
print("after updating:", student)
#-----------------------------------------------------------------------------------------------------------------
#3.deleting
student={
"name":"Elen",
"F_name":"Mosh",
"M_name":"Jack",
"age":21
}
print("Before deleting:",student)
del student["age"]
print("After deleting:",student)
#-----------------------------------------------------------------------------------------------------------------
#4. counting frequency of numbers
num=[2,3,4,5,2,3,4,3,6,7,6,6]
freq={}
for i in num:
if i in freq:
freq[i]+=1
else:
freq[i]=1
print(freq)
#--------------------------------------------------------------------------------------------------------------------
#5.find key with maximum value
scores={
"Language":89,
"Maths":98,
"Physics":99,
"Chemistry":86,
}
res=max(scores, key=scores.get)
print(res)
#----------------------------------------------------------------------------------------------------------------------
#6. merge two dictionaries
dict1={"apple":5,"orange":2}
dict2={"mango":7, "cherry":4}
merged= dict1.update(dict2)
print("After merging:", merged)
#-----------------------------------------------------------------------------------------------------------------------
#7.check if key exists
dict_1={
"Men":45
"Women:36
"Children":13
}
if "Children" in dict_1:
print("Key exists")
else:
print("Key does not exists")
#------------------------------------------------------------------------------------------------------------------------