Python’s indentation isn’t just about neat code — it determines whether your loops run as intended. Misaligned whitespace can lead to repeated prints, mixed outputs, or even broken scripts. Let’s break down two programs that reveal how indentation and loop control work in Python, and how a single space can change everything.
The power of indentation in Python loops
Python uses indentation to define code blocks. Unlike languages that rely on braces or semicolons, Python uses whitespace to group statements. This makes the language readable but also strict: a single misplaced space can push code inside or outside a loop.
Consider this simple loop that prints the number 1 five times:
count = 1
while count <= 5:
print(1, end=' ')
count = count + 1The program starts with count set to 1. The while loop checks if count is less than or equal to 5. Since it is, it prints 1 followed by a space instead of a newline (end=' '). Then it increments count by 1. The process repeats until count reaches 6, at which point the condition fails and the loop stops. The output is clean and predictable: 1 1 1 1 1.
Trying to print with end='\n' would force each 1 onto a new line, altering the output. But the indentation — the four spaces before print(1, end=' ') — ensures the print statement runs only once per loop iteration.
Loop behavior beyond basic counting
Now let’s examine a loop that prints even numbers from 2 to 10, then shows the final value of count and its double after the loop ends.
count = 1
while count <= 5:
print(count * 2, end=' ')
count = count + 1
print(count, end=' ')
print(count * 2)Inside the loop, count * 2 prints 2, 4, 6, 8, and 10, all on one line. Once the loop exits, count is 6. The next print(count, end=' ') outputs 6 (with a space), and the final print(count * 2) outputs 12 on a new line. The full output: 2 4 6 8 10 6 12.
This demonstrates how code outside the loop — aligned at the same level as the while statement — runs only once, after all iterations complete.
When indentation misfires: the cost of incorrect alignment
A small indentation error can turn a clean loop into a chaotic output generator. Here’s what happens when indentation isn’t respected:
count = 1
while count <= 5:
print(count * 2, end=' ')
count = count + 1
print(count)
print(count * 2)Because the last two print statements are indented to the same level as the loop body, they execute during every iteration. So in the first pass, it prints 2, then 2, then 2 again. In the second pass: 4, 4, 4. This continues, producing messy output like 2 2 4 4 6 6 8 8 10 10 6 6 12 12.
The loop never truly ends because the critical increment (count = count + 1) is buried under repeated prints. This is a classic example of how a single misplaced space can derail logic.
Key takeaways for clean Python loops
- Indentation defines scope: Use consistent spaces (typically four) to group loop bodies, conditionals, and functions. Never mix tabs and spaces.
- Control line endings with `end`: Use
end=' 'to avoid newlines and keep output compact. Useend='\n'or omit it to move to the next line. - Close loops with care: Place statements that should run once — like cleanup or final prints — at the same indentation level as the loop declaration.
- Test increment logic: Ensure your loop variable changes correctly and the exit condition will eventually be met.
Python’s indentation isn’t a stylistic choice — it’s a functional necessity. By mastering it, you prevent silent errors, maintain readable code, and ensure your loops behave as expected every time. Start small, test often, and let whitespace do the heavy lifting in structuring your logic.
AI summary
Python'da döngü ve girinti kavramlarını öğrenin, örnek programlar ve açıklamalar