-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1. Dictionary-Methods-and-Loops.py
78 lines (67 loc) · 1.59 KB
/
1. Dictionary-Methods-and-Loops.py
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
# details = {
# "name": "shiva",
# "age": 24,
# "gender": "male",
# 123: "prasad",
# }
# print(details)
#
# # Access Items values
# print(details["shiva"])
#
# # Add Items
# details["branch"] = "cse"
# print(details)
#
# # Remove Items
# # 1. pop()
# details.pop("gender")
# print(details)
# details.pop(123)
# print(details)
#
# # 2. popitem() it removes the last item in dictionary
# details.popitem()
# print(details)
#
# # 3. del() it removes the dictionary and if item is not specified then it deletes entire dictionary with items
# del details["name"]
# print(details)
# del details
# print(details) # returns error name details is not defined, because we deleted the dictionary
#
# # 4. clear() it empty the dictionary means removes al items and without deleting dictionary
# details.clear()
# print(details) # returns empty dictionary
#
# Change Items
# details["name"] = "prasad"
# print(details)
#
# update() it changes multiple item values at a time
# details.update({"name": "prasad","age": 99})
# print(details)
# Loop Dictionaries
details = {
"name": "shiva",
"age": 24,
"gender": "male",
123: "prasad",
}
# for item in details:
# print(item) # display only item key names
#
# for item in details:
# print(details[item]) # display only key values
# for item in details:
# print(f"{item} = {details[item]}") # display both key names and key values
# using methods
# 1. keys()
# for item in details.keys():
# print(item)
# 2. values()
# for item in details.values():
# print(item)
# 3. items()
# for item in details.items():
# print(item)