-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.py
76 lines (75 loc) · 2.55 KB
/
dictionary.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
# Dictionary
details = {"Arun": 52, "Prasanna": 78}
print(details)
details["Arun"] = 98
print(details)
details["Balaji"] = 75
details = {"Arun":52, "Prasanna":78}
print(details)
details["Arun"]=98
print(details)
details["Balaji"]=75
print(details)
del details["Arun"]
print(details)
print()
# Typecasting of compound datatype
print(list(details))
print(set(details))
print(tuple(details))
print()
# Extract from keys() and values() separated
print(details.keys()) # o/p : dict_keys(['Prasanna', 'Balaji'])
print(details.values()) # o/p : dict_values([78, 75])
# Extract from keys() and values() separated using * operator
print([*details.keys()]) # o/p : ['Prasanna', 'Balaji'']
print([*details.values()]) # o/p : [78, 75]
# get()
employeeDetails = {"name": "Arunkumar", "age": 24, "Address": "Ammachatram", "CGPA": 7.54}
print(details.keys()) # o/p : dict_keys(['Prasanna', 'Balaji'])
print(details.values()) # o/p : dict_values([78, 75])
# Extract from keys() and values() separated using * operator
print([*details.keys()]) # o/p : ['Prasanna', 'Balaji'']
print([*details.values()]) # o/p : [78, 75]
# get()
print(employeeDetails.get("CGPA"))
print(employeeDetails.get("city"))
# pop() & popitem()
print(employeeDetails.pop("Address"))
print(employeeDetails)
print(employeeDetails.popitem())
print(employeeDetails)
print()
# Print keys and values for using for loop
employeeDetails = {"name": "Arunkumar", "age": 24, "Address": "Ammachatram", "CGPA": 7.54}
for key in employeeDetails:
print(key, type(key))
for value in employeeDetails:
print(employeeDetails[value], type(value))
print()
# items()
employeeDetails = {"name": "Arunkumar", "age": 24, "Address": "Ammachatram", "CGPA": 7.54}
for key in employeeDetails.items():
print(key, type(key))
print()
for key, value in employeeDetails.items():
print(key, " : ", type(key))
print(value, " : ", type(value))
print()
# how to check key and value are present in dictionary?
employeeDetails = {"name": "Arunkumar", "age": 24, "Address": "Ammachatram", "CGPA": 7.54}
print("CGPA" in employeeDetails.keys())
print("GPA" in employeeDetails.keys())
print(24 in employeeDetails.values())
print(7.56 in employeeDetails.values())
print()
employeeDetails = {"name": "Arunkumar", "age" : 24, "Address" : "Ammachatram", "CGPA" : 7.54}
for key in employeeDetails:
print(key , type(key))
for value in employeeDetails:
print(employeeDetails[value] , type(value))
print()
# items()
employeeDetails = {"name": "Arunkumar", "age" : 24, "Address" : "Ammachatram", "CGPA" : 7.54}
for key in employeeDetails.items():
print(key , type(key))