3. Ver más…
Guttag, John. Introduction to
Computation and
Programming Using Python:
With Application to
Understanding Data Second
Edition. MIT Press, 2016.
ISBN: 9780262529624.
3
9. Repeticiones
while condición:
expresión
expresión
…
● La condición evalúa a bool
● Si la condición es True, se ejecutan todas las
instrucciones del bloque
● Se vuelve a evaluar la condición
● Se repite hasta que la condición sea False
Basado en: Ana Bell, Eric Grimson, and John Guttag. 6.0001 Introduction to Computer Science and Programming in Python. Fall 2016. Massachusetts
Institute of Technology: MIT OpenCourseWare, https://ocw.mit.edu. License: Creative Commons BY-NC-SA.
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lectu
re-slides-code/MIT6_0001F16_Lec1.pdf
14. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
15. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
16. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
2<=9
17. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
2
3<=9
18. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
2
3
4<=9
19. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
2
3
4
5<=9
20. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
2
3
4
5
6<=9
21. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
2
3
4
5
6
7<=9
22. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
2
3
4
5
6
7
8<=9
23. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
2
3
4
5
6
7
8
9<=9
24. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
2
3
4
5
6
7
8
9
9<=9
25. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
1
2
3
4
5
6
7
8
9
10<=9
26. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
Si
No
10<=9
F
a
l
s
e
1
2
3
4
5
6
7
8
9
27. Repeticiones en Python
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
i = 1
i<=9
print(i)
i = i + 1
print(“fin”)
Si
No
10<=9
F
a
l
s
e
1
2
3
4
5
6
7
8
9
fin
28. Repeticiones en Python
Versión Resumida:
for i in range(1, 10):
print(i)
i = 1
while i<=9:
print(i)
i = i + 1
print(“fin”)
29. Repeticiones en Python: for
for variable in range(numero):
expresión
expresión
…
● Cada vez que se ejecuta el loop, la variable toma un valor
● la primera vez, la variable comienza con el menor valor
● la próxima vez, la variable tiene el valor previo + 1
● …y se repite
Basado en: Ana Bell, Eric Grimson, and John Guttag. 6.0001 Introduction to Computer Science and Programming in Python. Fall 2016. Massachusetts
Institute of Technology: MIT OpenCourseWare, https://ocw.mit.edu. License: Creative Commons BY-NC-SA.
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lectu
re-slides-code/MIT6_0001F16_Lec1.pdf
30. El objeto range(start,stop,step)
● los valores por defecto son start = 0 y step = 1 y
son opcionales
● Iterar hasta que el valor sea stop - 1
Basado en: Ana Bell, Eric Grimson, and John Guttag. 6.0001 Introduction to Computer Science and Programming in Python. Fall 2016. Massachusetts
Institute of Technology: MIT OpenCourseWare, https://ocw.mit.edu. License: Creative Commons BY-NC-SA.
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lectu
re-slides-code/MIT6_0001F16_Lec1.pdf
31. for vs while
Basado en: Ana Bell, Eric Grimson, and John Guttag. 6.0001 Introduction to Computer Science and Programming in Python. Fall 2016. Massachusetts
Institute of Technology: MIT OpenCourseWare, https://ocw.mit.edu. License: Creative Commons BY-NC-SA.
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lectu
re-slides-code/MIT6_0001F16_Lec1.pdf
for while
● El número de iteraciones es conocido
● Usa un contador
● Se puede reescribir con loop while
● El número de iteraciones no está
restringido
● Puede usar un contador, pero debe
inicializarse fuera del loop e
incrementarse dentro
● No necesariamente es posible
reescribirlo como un loop for