forked from mouredev/Hello-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_dicts.py
More file actions
125 lines (89 loc) · 2.39 KB
/
07_dicts.py
File metadata and controls
125 lines (89 loc) · 2.39 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Clase en vídeo: https://youtu.be/Kp4Mvapo5kc
### Dictionaries ###
# Definición
my_dict = dict()
my_other_dict = {}
print(type(my_dict))
print(type(my_other_dict))
my_other_dict = {"Nombre": "Brais", "Apellido": "Moure", "Edad": 35, 1: "Python"}
my_dict = {
"Nombre": "Brais",
"Apellido": "Moure",
"Edad": 35,
"Lenguajes": {"Python", "Swift", "Kotlin"},
1: 1.77,
}
print(my_other_dict)
print(my_dict)
print(len(my_other_dict))
print(len(my_dict))
d = dict()
print(d)
print(type(d))
d = dict({1: "Ramesh", 2: "Suresh", 3: "Mahesh"})
print(d)
d = dict([(1, "Ramesh"), (2, "Arjun")])
print(d)
# Búsqueda
print(my_dict[1])
print(my_dict["Nombre"])
print("Moure" in my_dict)
print("Apellido" in my_dict)
# Inserción
my_dict["Calle"] = "Calle MoureDev"
print(my_dict)
# Actualización
my_dict["Nombre"] = "Pedro"
print(my_dict["Nombre"])
# Eliminación
del my_dict["Calle"]
print(my_dict)
# Otras operaciones
print(my_dict.items())
print(my_dict.keys())
print(my_dict.values())
my_list = ["Nombre", 1, "Piso"]
my_new_dict = dict.fromkeys((my_list))
print(my_new_dict)
my_new_dict = dict.fromkeys(("Nombre", 1, "Piso"))
print((my_new_dict))
my_new_dict = dict.fromkeys(my_dict)
print((my_new_dict))
my_new_dict = dict.fromkeys(my_dict, "MoureDev")
print((my_new_dict))
my_values = my_new_dict.values()
print(type(my_values))
print(my_new_dict.values())
print(list(dict.fromkeys(list(my_new_dict.values())).keys()))
print(tuple(my_new_dict))
print(set(my_new_dict))
d = {1: "Ramesh", 2: "Suresh", 3: "Mahesh"}
print("Before clearing dictionary: ", d)
d.clear()
print("After cleared entries in dictionary: ", d)
d = {1: "Ramesh", 2: "Suresh", 3: "Mahesh"}
print("Before delete dictionary: ", d)
del d
d = dict([(1, "Ramesh"), (2, "Arjun")])
print("length of dictionary is: ", len(d))
d = {1: "Ramesh", 2: "Suresh", 3: "Mahesh"}
print(d.get(1))
print(d.get(100))
d = {1: "Ramesh", 2: "Suresh", 3: "Mahesh"}
print(d.get(1))
print(d.get(100, "No key found"))
d = {1: "Ramesh", 2: "Suresh", 3: "Mahesh"}
print("Before pop:", d)
d.pop(1)
print("After pop:", d)
# This method removes an arbitrary item(key-value) from the dictionary and returns it.
d = {1: "Ramesh", 2: "Suresh", 3: "Mahesh"}
print("Before popitem:", d)
d.popitem()
print("After popitem:", d)
d1 = {1: "Ramesh", 2: "Suresh", 3: "Mahesh"}
d2 = d1.copy()
print(d1)
print(d2)
squares = {a: a * a for a in range(1, 6)}
print(squares)