Counting loops · Contar bucles
Repeat with for
- A
forloop repeats code a fixed number of times. - The body of the loop is indented, like an
if. - A loop variable holds a new value on each pass.
Repetir con for
- Un bucle
forrepite el código un número fijo de veces. - El cuerpo del bucle está indentado, como en un
if. - Una variable de bucle toma un nuevo valor en cada iteración.
Counting with range
range(5)gives the numbers0, 1, 2, 3, 4(stops before 5).range(1, 6)gives1, 2, 3, 4, 5(start included, stop not).range(0, 10, 2)counts in steps of 2.
Contar con range
range(5)produce los números0, 1, 2, 3, 4(se detiene antes del 5).range(1, 6)produce1, 2, 3, 4, 5(inicio incluido, fin excluido).range(0, 10, 2)cuenta en saltos de 2.
for i in range(5):
print(i)
Build a total (accumulator)
- An accumulator is a variable that grows inside a loop.
- Start it at
0before the loop. - Add to it on each pass to build a running total.
Construir un total (acumulador)
- Un acumulador es una variable que crece dentro de un bucle.
- Inícielo en
0antes del bucle. - Súmale valores en cada iteración para construir un total acumulado.
total = 0
for n in range(1, 6):
total = total + n
print(total)
In Cambridge pseudocode
- A Python
for … rangeloop is the exam's count-controlledFOR … NEXT.
En pseudocódigo de Cambridge
- Un bucle
for … rangede Python es el bucle controlado por conteo del examenFOR … NEXT.
Total ← 0
FOR I ← 1 TO 5
Total ← Total + I
NEXT I
OUTPUT Total
Words and a number on one line
- A comma inside
printputs a space between the parts. print("7 x", 1, "=", 7 * 1)prints7 x 1 = 7.+cannot join a number to text. Use the comma, or turn the number into text withstr.
Palabras y un número en una línea
- Una coma dentro de
printcoloca un espacio entre las partes. print("7 x", 1, "=", 7 * 1)imprime7 x 1 = 7.+no puede unir un número a texto. Use la coma, o convierta el número a texto constr.
Common mistakes
range(n)counts0ton - 1, not1ton.range(a, b)stops beforeb.- The loop body must be indented under the
forline.
Errores comunes
range(n)cuenta desde0hastan - 1, no desde1hastan.range(a, b)se detiene antes deb.- El cuerpo del bucle debe estar indentado bajo la línea del
for.
Now you try
- Use a
forloop withrange(...)in each task. - Press Check answer to test your code.
Ahora tú intentas
- Usa un bucle
forconrange(...)en cada tarea. - Presiona Check answer para probar tu código.
A counting loop, pass by pass · Un bucle de conteo, paso a paso
A for loop repeats its body; watch the counter and the running total · suma acumulativa. · Un bucle for repite su cuerpo; observa el contador y el total acumulado.
Use a for loop to print the numbers 1 to · hasta 5, each on its own line. · Usa un bucle for para imprimir los números del 1 al 5, cada uno en una línea distinta.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Use a loop and an accumulator to add up every whole number from 1 to · hasta 100. Store the total in total. · Usa un bucle y un acumulador para sumar todos los números enteros del 1 al 100. Almacena el total en total.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Multiply together every whole number from 1 to · hasta 5 (that is 1×2×3×4×5). Start product at 1 and store the result there. · Multiplica todos los números enteros del 1 al 5 (es decir, 1×2×3×4×5). Inicia product en 1 y almacena el resultado allí.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Print exactly one line: 7 x 1 = 7. Finish the print that is already started. A comma between the parts prints them with spaces: print("7 x", 1, "=", 7 * 1). · Imprime exactamente una línea: 7 x 1 = 7. Termina el print que ya ha sido iniciado. Una coma entre las partes los imprime con espacios: print("7 x", 1, "=", 7 * 1).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Use a for loop to print the first five lines of the 7 times table: 7 x 1 = 7, 7 x 2 = 14, up to 7 x 5 = 35. The print from the last task is already inside the loop — change the 1 into the loop variable. · Usa un bucle for para imprimir las primeras cinco líneas de la tabla del 7: 7 x 1 = 7, 7 x 2 = 14, hasta 7 x 5 = 35. El print de la tarea anterior ya está dentro del bucle: cambia el 1 por la variable del bucle.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.