Skip to main content

How to Repeat Code a Number of Times in Python with for and range

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Roll a die five times, print three lines, try something ten times: often the same code has to run a set number of times. In Python, a for loop over range does that, and an underscore says you don't need the turn's number. This guide does it on Python 3.15.2.

Syntax​

Syntax
for _ in range(<times>):
<code to repeat>
PartWhat it means
for ... in ...:Runs the indented lines once for every value it gets
range(<times>)Hands out that many numbers, one per turn, starting at 0
_The name that receives each number. An underscore is a normal name, used by habit when the number isn't needed
<code to repeat>The lines that run on every turn, indented by four spaces

The Python tutorial introduces range like this: "If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy." About the underscore, the language reference says "_ is a regular identifier", and adds: "It is also commonly used for unused variables."

The Example​

A knock-knock joke starts with three knocks. The script prints Knock three times, then one line that isn't part of the loop.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

knock.py
for _ in range(3):
print("Knock")

print("Who's there?")

The knock.py script in a macOS-style code window: a for loop over range(3) with an underscore as its name that prints Knock, then a line that prints Who's there?

The indented print("Knock") belongs to the loop. The last line isn't indented, so it runs once, after the loop.

Step 2: Run It​

Terminal
uv run python knock.py
Output
Knock
Knock
Knock
Who's there?

The terminal after uv run python knock.py: Knock three times, then Who's there?

range(3) handed out three numbers, so the loop ran three times and printed Knock on each turn. Then the loop was over and the last line ran. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Looping over the number itselffor _ in 3: raised TypeError: 'int' object is not iterableWrap it in range: for _ in range(3):
Giving range a number with a decimal pointrange(2.5) raised TypeError: 'float' object cannot be interpreted as an integerUse a whole number
Expecting the numbers to start at 1A loop that printed its number gave 0, 1 and 2Count from 1 with range(1, 4), which gave 1, 2 and 3

FAQ​

What if I need the number of the turn?​

Give it a real name instead of the underscore. for turn in range(3): with print("turn", turn) printed turn 0, turn 1 and turn 2. Use _ only when the loop's lines never look at the number.

Is the underscore special?​

No. It's an ordinary name that Python fills like any other. After for _ in range(3): had finished, print(_) gave 2, the last number it received. Writing _ tells a reader that you won't use the value.

Can the number of turns come from a variable?​

Yes. With times = 2, the loop for _ in range(times): ran twice.

What happens with range(0)?​

The loop doesn't run at all, and Python raises no error. The same goes for a negative number: range(-2) ran the loop zero times.

How do I count how often something happened in the loop?​

Keep a number outside the loop and add to it on every turn. With knocks = 0 before the loop and knocks = knocks + 1 inside it, knocks was 3 after three turns.

Conclusion​

for _ in range(3): ran its indented line three times and printed Knock on each turn. range sets the number of turns, and the underscore is a plain name that shows the turn's number isn't used.

Additional Resources​