-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
45 lines (41 loc) · 1.29 KB
/
Copy pathdictionary.py
File metadata and controls
45 lines (41 loc) · 1.29 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
#dictionary
birthday={ #dictionary
"darshan": "24-11-2006", #key: value
"deekshitha": "20-03-2007", #key: value
"aishu": "17-11-2008" #key: value
}
meaning={
"pen": "used to write", #key: value
"books": "used to read", #key: value
"chair": "used to sit" #key: value
}
print(birthday["darshan"]) #accessing dictionary
print(birthday.get("aishu"))
print(birthday.get("shwetha","not found")) #for safe accessing
birthday["prasad"]="20-07-1978" #adding value in run time
print(birthday)
birthday["deekshitha"]="20-03-2003" #updating the value
print(birthday)
birthday.pop("aishu") #to remove particular data use key
print(birthday)
del birthday["deekshitha"] #another way of deleting
print(birthday)
#dictionary methods
print(birthday.keys()) #to get only keys
print(birthday.values()) #to get only valuess
print(birthday.items()) #to get both key and values items() is used
new_item={"bed": " used to sleep"}
meaning.update(new_item) #updating new item
print(meaning)
#list cannot be used in dict but we can use int,string,float,boolean
d1={
"name": "sugar",
"weight": 1,
"price": 50
}
d2={
"name": "ghee",
"weight": 2,
"price": 150
}
print(f"total weight:{d1["weight"]+d2["weight"]}")#to get the total weight from d1 and d2