forked from ELC/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter3.py
More file actions
201 lines (149 loc) · 4.51 KB
/
chapter3.py
File metadata and controls
201 lines (149 loc) · 4.51 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
####################################################
# 3. Control de Flujo
####################################################
una_variable = 5
# 3.1 Bloque IF | Decisión
if una_variable > 10:
print("Valor mayor a 10")
if una_variable:
print("La variable no debe ser 0")
# Else
if una_variable >= 0:
print("El valor es positivo")
else:
print("El valor es negativo")
# Multiples condiciones
if una_variable % 2 == 0:
print("El valor es par")
elif una_variable < 0:
print("El valor es impar y negativo")
elif una_variable > 100:
print("El valor es impar y mayor a 100")
else:
print("El valor no cumple las condiciones")
# Operador Ternario
edad = 30
mayor_de_edad = "Si" if edad >= 18 else "No"
# Equivalente en C/Java/Net a mayor_de_edad = edad >= 18 ? "Si" : "No"
####################################################
# 3.2 Bucles | For
####################################################
list(range(4)) # => [0, 1, 2, 3]
for i in range(4):
print(i)
# 0
# 1
# 2
# 3
for animal in ["perro", "gato", "raton"]:
print(f"{animal} es un mamifero")
# perro es un mamifero
# gato es un mamifero
# raton es un mamifero
nombres = ["Juan", "Pedro", "Maria"]
for indice, nombre in enumerate(nombres): # Enumerate agrega indices
print(f"{indice} - {nombre}")
# 0 - Juan
# 1 - Pedro
# 2 - Maria
edades = [60, 15, 84]
for nombre, edad in zip(nombres, edades): # Zip combina listas
print(f"{nombre} tiene {edad} años")
# Juan tiene 60 años
# Pedro tiene 15 años
# Maria tiene 84 años
# Zip y Enumerate pueden combinarse
for indice, (nombre, edad) in enumerate(zip(nombres, edades)):
print(f"{indice} - {nombre} tiene {edad} años")
# 0 - Juan tiene 60 años
# 1 - Pedro tiene 15 años
# 2 - Maria tiene 84 años
alumnos = {"Juan": 60, "Pedro": 15, "Maria": 84}
for nombre, edad in alumnos.items():
print(f"{nombre} tiene {edad} años")
# Juan tiene 60 años
# Pedro tiene 15 años
# Maria tiene 84 años
# Nombre: [Edad, Nota]
alumnos = {"Juan": [60, 7.5], "Pedro": [15, 4.1], "Maria": [84, 9.5]}
for nombre, (edad, nota) in alumnos.items():
if nota >= 6:
print(f"{nombre} aprobó con {nota} puntos, teniendo {edad} años")
# Juan aprobó con 7.5 puntos, teniendo 60 años
# Maria aprobó con 9.5 puntos, teniendo 84 años
buscado = "Pedro"
for nombre, (_, nota) in alumnos.items():
if nombre == buscado:
print(f"{nombre} obtuvo {nota} puntos")
break
# Pedro obtuvo 4.1 puntos
buscado = "Martín"
for nombre, (_, nota) in alumnos.items():
if nombre == buscado:
print(f"{nombre} obtuvo {nota} puntos")
break
else:
print("No existe ese alumno")
# No existe ese alumno
precios = [6.43, 7.94, 9.23, 7.97, 4.84, 9.71, 6.52, 8.94, 8.62, 9.72]
for indice, precio in enumerate(precios):
if precio <= 8.0:
continue
precios[indice] *= 0.8
precios # => [6.43, 7.94, 7.38, 7.97, 4.84, 7.77, 6.52, 7.15, 6.9 , 7.78]
####################################################
# 3.2 Bucles | While
####################################################
# El bucle while opera identicamente al bucle for con break, continue y else
# Bucle While tradicional
x = 0
while x < 4:
print(x)
x += 1
# Bucle Do-While
x = 0
while True:
print(x)
x += 1
if x < 4:
break
####################################################
# 3.3 Excepciones | Try Except Else Finally
####################################################
try:
a = 1 / 0
except ZeroDivisionError as exception:
print(f"Ha ocurrido un error | {exception}")
# Ha ocurrido un error | division by zero
try:
a = 1 / 0
except ZeroDivisionError as exception:
print(f"Ha ocurrido un error | {exception}")
finally:
print("Proceso finalizado")
# Ha ocurrido un error | division by zero
# Proceso finalizado
# Algunas excepciones comunes
# Lista completa: https://docs.python.org/3/library/exceptions.html
try:
lista = [1, 2, 3]
lista[3]
except IndexError as exception:
print(f"No pueden utilizarse índices fuera del rango | {exception}")
try:
a = 1 / 0
except ZeroDivisionError as exception:
print(f"No puede dividirse por cero | {exception}")
try:
notas = {"Juan": 2, "María": 3}
notas["Alejandro"]<
except KeyError as exception:
print(f"Sólo pueden usarse claves definidas | {exception}")
try:
print(hola)
except NameError as exception:
print(f"Sólo pueden usarse variables definidas | {exception}")
try:
a, b = [1, 2, 3]
except ValueError as exception:
print(f"Valores provistos no coinciden con experados | {exception}")