Loops repeat an action for you.
A loop repeats a block of code, so you do not have to write the same instruction again and again. Use for to repeat once for each value, and while to repeat while a condition is True. Each pass through a loop is called an iteration.
for number in range(5):
print(number)range(5) produces 0, 1, 2, 3, and 4. First, number gets 0 and Python runs the indented block. Then number gets 1 and the block runs again. This continues through 4; when there are no more values, the loop ends. number is a variable whose value changes on each iteration.
for → once for each value
while → repeat while a condition is TrueA for loop follows values one by one. A while loop keeps checking a condition. In both cases, Python runs the indented block, then either moves to the next value or checks the condition again.
Indentation still matters: As in Conditions, indentation tells Python which instructions belong to the block. Every indented line in a for or while loop repeats; code outside the indentation runs after the loop finishes.
print("Hello")
print("Hello")
print("Hello")
# The same action with a loop
for number in range(3):
print("Hello")Without a loop, you repeat the same instruction yourself. With a loop, Python repeats the indented block for you. The point is not the word "Hello"; it is that one block can run many times.
for number in range(3):
print(number)
print("Inside the loop")
print("Loop finished")Both indented print() lines run during every iteration. print("Loop finished") is not indented, so it is not part of the loop and runs once after the loop ends.
range(5) # 0, 1, 2, 3, 4
range(2, 5) # 2, 3, 4
range(0, 10, 2) # 0, 2, 4, 6, 8With one argument, range(5) starts at 0 and stops before 5. With start and stop, range(2, 5) includes 2 but excludes 5. A third argument is the step: range(0, 10, 2) moves forward by 2 each time. Think: start included → stop excluded → step controls the jump.
count = 1
while count <= 3:
print(count)
count += 1A while loop connects directly to Conditions: Python checks count <= 3. If it is True, Python runs the indented block, changes count, and checks again. When the condition becomes False, the loop stops. count += 1 is shorthand for count = count + 1.
count = 1
while count > 0:
print(count)count starts as 1, so count > 0 is True. Nothing inside this loop changes count, so the condition stays True forever. When you write a while loop, ask: what will change so that this condition can eventually become False?
for number in range(5):
if number == 3:
break
print(number)break immediately ends the whole loop. The flow is 0 → 1 → 2 → 3 → break → STOP. When number == 3, Python runs break and never continues to 4.
for number in range(5):
if number == 2:
continue
print(number)continue skips the rest of this iteration and moves to the next value. The flow is 0 → 1 → 2 → continue → 3 → 4. Unlike break, it does not end the loop: break stops the loop, while continue skips one iteration.
total = 0
for number in range(1, 4):
total = total + number
print(total)The value of total stays between iterations. Start with total = 0; when number = 1, total becomes 1; then number = 2 makes it 3; and number = 3 makes it 6. The final output is 6.
for number in range(3):
print(number)
while count <= 3:
print(count)
count += 1Use for when you have a sequence or range of values to go through. Use while when repetition depends on a condition staying True. A helpful first question is: do I have values to go through, or a condition to keep checking?
