Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/marioaje/Python/llms.txt

Use this file to discover all available pages before exploring further.

Loops are how programs do repetitive work without repeating code. Python gives you two fundamental loop constructs: the for loop, which iterates over a sequence a known number of times, and the while loop, which keeps running as long as a condition remains True. Understanding when to use each — and how to control them — is essential for writing efficient Python programs.

for Loop with range()

The for loop is Python’s counting loop. When paired with the built-in range() function it runs a block of code a precise number of times, giving you access to the current counter value on every iteration.

range(start, stop, step)

ArgumentMeaningDefault
startFirst value produced (inclusive)0
stopUpper bound (exclusive — never produced)required
stepHow much to add each iteration1

Ascending counter

for.py
# Ascending counter: 1, 2, 3
for conti in range(1, 4, 1):
    print(conti)
Output:
1
2
3

Descending counter

Pass a negative step to count backwards:
for.py
# El for es como un contador, este ciclo tiene un inicio y un final
# contador descendente
contador = 0

for conti in range(4, 1, -1):
    contador = contador + 1
    contador += 1   # (contador + 1)

    print(conti)

print("Cantidad de elementos", contador)
Output:
4
3
2
Cantidad de elementos 6
Notice that contador is incremented twice per iteration (contador = contador + 1 and then contador += 1), so after 3 passes through the loop it reaches 6.
The += operator is shorthand for adding a value to a variable in place. contador += 1 is exactly the same as writing contador = contador + 1. This works for all arithmetic operators: -=, *=, /=, //=, **=, and %=. It keeps your code shorter and easier to read.

for-in Loop (foreach-style)

When you need to visit every element of a list (or any other iterable) without caring about the index, use the for item in collection: pattern. This is Python’s equivalent of a foreach loop in other languages.
foreach.py
# foreach: permite recorrer una lista que desconozcamos sus valores
mylista = ['mario', "Jimenez", 44, 10.1, True]

for item in mylista:
    print("Key o elemento de la lista", item)
Output:
Key o elemento de la lista mario
Key o elemento de la lista Jimenez
Key o elemento de la lista 44
Key o elemento de la lista 10.1
Key o elemento de la lista True
Python lists can hold values of mixed types — strings, integers, floats, and booleans all coexist in mylista. The loop variable item takes on each value in order, one per iteration.

while Loop

A while loop runs its body repeatedly as long as its condition evaluates to True. Unlike for, you control the counter yourself — which makes while ideal when the number of iterations is not known in advance.
while.py
# validacion de un ciclo, siempre que cumpla la condicion
# contador ascendente simple
contador = 0

while contador < 10:
    print("Numero de secuencia", contador)
    contador += 1
Output:
Numero de secuencia 0
Numero de secuencia 1
...
Numero de secuencia 9
The course also demonstrates a range-based while that counts from a negative starting point all the way up to 333:
while.py
condicion = -100

while condicion <= 333:
    print(condicion)
    condicion += 1
This loop will print every integer from -100 to 333 inclusive — a total of 434 iterations — before condicion becomes 334 and the condition condicion <= 333 turns False, ending the loop.
Beware of infinite loops. If the condition of a while loop never becomes False, your program will run forever (or until forcibly stopped). Always make sure something inside the loop body moves the program closer to the exit condition.
# ⚠️ This loop never ends — Opcion is never changed inside the body
Opcion = 0
while Opcion == 0:
    print('Dentro del while')
    # Opcion = 1   ← uncommenting this line would fix it
If you accidentally write an infinite loop, press Ctrl + C in your terminal to interrupt execution.

Build docs developers (and LLMs) love