-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_anidacion.py
166 lines (129 loc) · 4 KB
/
02_anidacion.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# LISTA DE DICCIONARIOS
alien_0 = {"color": "green", "points": 5}
alien_1 = {"color": "yellow", "points": 10}
alien_2 = {"color": "red", "points": 15}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
print(alien)
""" se imprime lo siguiente:
{'color': 'green', 'points': 5}
{'color': 'yellow', 'points': 10}
{'color': 'red', 'points': 15}
"""
# crear 30 aliens verdes
aliens = []
for alien_number in range(30):
new_alien = {"color": "green", "points": 5, "speed": "slow"}
aliens.append(new_alien)
# mostrar los 5 primeros
for alien in aliens[:5]:
print(alien)
print("...")
# mostrar la cantidad de aliens creada
print(f"\nTotal number of aliens: {len(aliens)}")
""" con este código se imprime lo siguiente:
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
...
Total number of aliens: 30
"""
# si se quisiera modificar el valor de los 3 primeros, se podría hacer lo siguiente
for alien in aliens[:3]:
if alien["color"] == "green":
alien["color"] = "yellow"
alien["points"] = 10
alien["speed"] = "medium"
# mostrar los 5 primeros
for alien in aliens[:5]:
print(alien)
print("...")
# mostrar la cantidad de aliens creada
print(f"\nTotal number of aliens: {len(aliens)}")
""" con este código se imprime lo siguiente:
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
...
Total number of aliens: 30
"""
# DICCIONARIO CON LISTA
favorite_languages = {
"jen": ["python", "ruby"],
"sarah": ["c"],
"edward": ["ruby", "go"],
"phil": ["python", "haskell"]
}
for name, languages in favorite_languages.items():
if len(languages) == 1:
print(f"\n{name.title()}'s favorite language is:")
else:
print(f"\n{name.title()}'s favorite languages are:")
for language in languages:
print(f"\t- {language.title()}")
""" este código imprime lo siguiente:
Jen's favorite languages are:
- Python
- Ruby
Sarah's favorite language is:
- C
Edward's favorite languages are:
- Ruby
- Go
Phil's favorite languages are:
- Python
- Haskell
"""
# DICCIONARIO DENTRO DE DICCIONARIO
users = {
"aeinstein": {
"first_name": "albert",
"last_name": "einstein",
"location": "princeton"
},
"mcurie": {
"first_name": "marie",
"last_name": "curie",
"location": "paris"
}
}
for username, user_info in users.items():
print(f"\nUsername: {username}")
full_name = f"{user_info['first_name']} {user_info['last_name']}"
location = user_info["location"]
print(f"\tFull name: {full_name.title()}")
print(f"\tLocation: {location.title()}")
""" se imprime lo siguiente:
Username: aeinstein
Full name: Albert Einstein
Location: Princeton
Username: mcurie
Full name: Marie Curie
Location: Paris
"""
# ACCEDER A LOS VALORES SIN USAR BUCLES
favorite_languages = {
"jen": ["python", "ruby"],
"sarah": ["c"],
"edward": ["ruby", "go"],
"phil": ["python", "haskell"]
}
list_of_fav_languages = list(favorite_languages.items())
print(list_of_fav_languages)
""" se imprime lo siguiente:
[
('jen', ['python', 'ruby']),
('sarah', ['c']),
('edward', ['ruby', 'go']),
('phil', ['python', 'haskell'])
]
"""
# vemos que es una lista de tuplas -> podemos acceder a los datos de esta forma
print(list_of_fav_languages[0]) # ('jen', ['python', 'ruby']) -> tupla entera
print(list_of_fav_languages[0][0]) # jen -> nombre de la persona
print(list_of_fav_languages[0][1]) # ['python', 'ruby'] -> lenguajes favoritos
print(list_of_fav_languages[0][1][0]) # python -> uno de los lenguajes favoritos