-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
34 lines (28 loc) · 1.08 KB
/
dictionary.py
File metadata and controls
34 lines (28 loc) · 1.08 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
# test = {} # this will create empty disctionary/json object
# studentDetails = {
# "marks": {
# "Parth": 75,
# "Shivu": 95,
# "Test": 35
# },
# "attendance": {
# "Parth": "70%",
# "Shivu": "97%",
# "Test": "30%"
# }
# }
studentMarksDetails = {
"Parth": 75,
"Shivu": 95,
"Test": 35
}
print(studentMarksDetails)
print(studentMarksDetails.items()) # returns items of disctionary/json object
print(studentMarksDetails.keys()) #returns keys
print(studentMarksDetails.values()) #returns values
studentMarksDetails.update({"Parth": 100, "Shivansh": 99}) # updates existing element and/or add new if not exists
#print(studentMarksDetails)
# below is the diff between get and [] of printing disctionary/json object in python
print(studentMarksDetails.get("Parth1")) # returns 100 if matches, if not then returns 'None'
print(studentMarksDetails["Parth1"]) # returns 100 if matches, if not then returns error
# but the diff is when the key not exists, get() will return 'None' while [] will returns an error