Python Loops: For, While, Break, and Continue
Table of Contents
Loops let you run the same code over and over without rewriting it each time. Whether you need to count through numbers, process items in a list, or keep going until a condition changes, Python gives you for loops and while loops to handle it.
All the content from our Boot.dev courses are available for free here on the blog. This one is the “Loops” chapter of Learn to Code in Python. If you want to try the far more immersive version of the course, do check it out!
How Does a For Loop Work in Python?
Let’s say you want to print the numbers 0 through 9. You could write ten print statements:
print(0)
print(1)
print(2)
# ... you get the idea
print(9)
A for loop saves you all that typing — especially if you wanted to do the same thing a thousand or a million times:
for i in range(0, 10):
print(i)
The variable i takes on each value from 0 to 9, one at a time. In plain English:
- Start with
iequals0 - If
iis greater than or equal to10, exit the loop. Otherwise:- Print
ito the console - Add
1toi - Go back to step 2
- Print
The numbers in range(a, b) are inclusive of a and exclusive of b, so range(0, 10) includes 0 but not 10.
How Does range() Work in Python?
The range() function actually takes up to three parameters: range(start, stop, step).
The step parameter controls how much i changes each iteration. By default it’s 1, but you can change it:
for i in range(0, 10, 2):
print(i)
# 0
# 2
# 4
# 6
# 8
You can even go backwards with a negative step:
for i in range(3, 0, -1):
print(i)
# 3
# 2
# 1
This is useful any time you need to count down or iterate in reverse.
Why Does Whitespace Matter in Python Loops?
The body of a for loop must be indented, otherwise you’ll get a SyntaxError. Every line in the body needs to be indented the same way — the convention is 4 spaces. Pressing the <tab> key should insert 4 spaces automatically in most editors.
This is the same indentation rule that applies to if statements, functions, and every other block in Python. Whitespace matters.
How Do While Loops Work in Python?
A while loop keeps running as long as its condition is True:
while 1:
print("1 evaluates to True")
# prints forever (infinite loop)
That example runs forever. Normally, a while loop’s condition involves a comparison that eventually becomes False:
num = 0
while num < 3:
num += 1
print(num)
# 1
# 2
# 3
The loop stops when num reaches 3 because 3 < 3 is False. Use a while loop when you don’t know ahead of time how many iterations you’ll need — for example, reading input until a user types “quit”, or processing data until a counter hits a limit.
What Does continue Do in a Python Loop?
The continue statement skips the rest of the current iteration and jumps straight to the next one. Whatever else was supposed to happen in that iteration gets skipped.
For example, if you want to skip negative numbers when calculating square roots:
for number in range(-5, 5):
if number < 0:
continue
print(f"The square root of {number} is {number ** 0.5}")
The square root of 0 is 0.0
The square root of 1 is 1.0
The square root of 2 is 1.4142135623730951
The square root of 3 is 1.7320508075688772
The square root of 4 is 2.0
Using continue to avoid pointless work can make your code faster, especially when the loop body includes expensive computations.
What Does break Do in a Python Loop?
The break statement exits the loop entirely — not just the current iteration, but the whole thing:
for n in range(42):
print(f"{n} * {n} = {n * n}")
if n * n > 150:
break
This would loop from 0 all the way to 41, but it exits early. Once n * n is greater than 150, the break executes and the loop stops. The last line printed is 13 * 13 = 169.
break is handy when you’re searching for something and want to stop as soon as you find it, rather than continuing through the rest of the sequence.
What Should You Learn After Python Loops?
Loops and conditionals together give you full control over your program’s flow. The next step is usually lists — loops and lists go hand in hand since you’ll often loop over collections of data.
If you want to keep going through the full Python curriculum with hands-on exercises, check out the Learn to Code in Python course on Boot.dev.
Frequently Asked Questions
What is the difference between a for loop and a while loop in Python?
A for loop iterates over a known sequence like a range of numbers or a list. A while loop keeps running as long as a condition is True. Use a for loop when you know how many iterations you need, and a while loop when you don't.
Is range() inclusive or exclusive in Python?
The start value is inclusive and the end value is exclusive. So range(0, 10) includes 0 but does not include 10. This is a common convention in programming.
What is the difference between break and continue in Python?
continue skips the rest of the current iteration and jumps to the next one. break exits the entire loop immediately. Both work in for loops and while loops.
Does Python have a foreach loop?
Python does not have a separate foreach keyword. The for loop handles this directly. Writing for item in my_list iterates over each element without needing an index, which is effectively the same as foreach in other languages.
Related Articles
Python If Statements: If, Else, and Elif
Mar 03, 2026 by lane
Every useful program needs to make decisions. Python’s if statement is how you tell your code to do one thing or another depending on some condition — and once you understand if, elif, and else, you can handle just about any branching logic.
Python Math: Operators and Numbers Explained
Mar 02, 2026 by lane
Python has excellent built-in support for math operations — from basic arithmetic to exponents to bitwise logic. You don’t need to import anything to do most math in Python, which is one reason it’s so popular for everything from backend development to data science.
Python Functions: How to Define and Call Them
Mar 01, 2026 by lane
Functions allow you to reuse and organize code. Instead of copying and pasting the same logic everywhere, you define it once and call it whenever you need it. They’re the most important tool for writing DRY code.
Python Variables: A Complete Beginner Guide
Feb 28, 2026 by lane
Variables are how we store data as our program runs. You’re probably already familiar with printing data by passing it straight into print(), but variables let us save that data so we can reuse it and change it before printing it. This guide covers everything you need to know about Python variables: creating them, naming them, and understanding the basic data types they can hold. If you’re just getting started, you might also want to know why Python is worth learning in the first place.