Bucles: for y while
Los computadores son buenísimos para hacer lo mismo mil veces sin aburrirse. Acá aprendes a aprovecharlo.
Loops: for and while
Computers are brilliant at doing the same thing a thousand times without getting bored. Here you learn to use that.
Para repetir un número conocido de veces se usa for junto con
range(), que genera una secuencia de números.
Comparado con otros lenguajes, esto es notablemente más simple: no hay contador que inicializar ni que hacer avanzar a mano.
To repeat a known number of times you use for with range(), which
generates a sequence of numbers.
Compared to other languages this is notably simpler: no counter to initialise, nothing to advance by hand.
Fíjate que empieza en 0 y termina en 4, no en 5.
range(5) significa "cinco números empezando desde cero". Esto confunde al
principio, pero es coherente con cómo se numeran las listas, que verás en el próximo tutorial.
range() acepta hasta tres datos:
Note it starts at 0 and ends at 4, not 5.
range(5) means "five numbers starting from zero". Confusing at first, but
consistent with how lists are numbered, which you will see next.
range() takes up to three values:
Acá está lo bonito de Python: for no sirve solo para contar. Recorre
directamente cualquier colección — una lista, un texto, lo que sea.
Se lee literal: "para cada jugador en jugadores".
Here is the nice part of Python: for is not just for counting. It goes directly
through any collection — a list, a text, anything.
It reads literally: "for each player in players".
for sirve cuando sabes cuántas vueltas. while sirve cuando
no lo sabes: repite mientras se cumpla una condición.
for is for when you know how many passes. while is for when you
do not: it repeats while a condition holds.
Ese {altura:.1f} dentro de la f-string muestra un solo
decimal. Y no sabíamos de antemano que serían 6 botes: el programa lo descubrió solo.
That {altura:.1f} inside the f-string shows a single
decimal. And we did not know beforehand it would be 6 bounces: the program worked it out.
Dos palabras que dan control fino dentro de un bucle:
break— sale del bucle de inmediato.continue— se salta esta vuelta y pasa a la siguiente.
Two words giving fine control inside a loop:
break— leaves the loop immediately.continue— skips this pass and moves to the next.
El símbolo % no es porcentaje: es el resto de
la división. Un número es par si n % 2 == 0. Se usa muchísimo.
The % symbol is not percentage: it is the
remainder. A number is even if n % 2 == 0. Used constantly.
for con range(), recorrer colecciones directamente,
while, break, continue y el operador %.
Ya usaste una lista de pasada. Ahora la vemos en serio.
for with range(), looping over collections directly,
while, break, continue and the % operator.
You already used a list in passing. Now we look at it properly.