Syntax
range(start, stop, step)Examples
Basic Range
Generating a sequence from 0 up to (but not including) a number.
for i in range(5):
print(i) # 0, 1, 2, 3, 4Range with Start and Stop
Specifying both where to begin and where to stop.
for i in range(2, 8):
print(i) # 2, 3, 4, 5, 6, 7Range with Step
Controlling the interval between numbers, including counting backward with a negative step.
for i in range(0, 10, 2):
print(i) # 0, 2, 4, 6, 8
for i in range(10, 0, -2):
print(i) # 10, 8, 6, 4, 2Converting Range to a List
range() produces a lazy sequence object, not a list - convert it explicitly if you need a real list.
r = range(5)
print(r) # range(0, 5)
print(list(r)) # [0, 1, 2, 3, 4]
# Useful when you need indices for a list
items = ["a", "b", "c"]
print(list(range(len(items)))) # [0, 1, 2]Best practices
- Remember range(stop) starts at 0 by default, and the stop value itself is never included in the sequence
- Use range() directly in a for loop rather than converting it to a list first - it's more memory-efficient since it generates values lazily
- Use a negative step to count downward, e.g. range(10, 0, -1) for a countdown
- Prefer enumerate() over range(len(list)) when you actually need both the index and the corresponding item
At a glance
- Purpose
- Scripting and general-purpose applications
- File extension
- .py
- Runs in
- Python interpreter
- Usually used with
- Python standard library and packages
Specifications & further reading
Related Python documentation
if
The if statement is Python's primary conditional control structure. It allows your program to make decisions by executing different code blocks based on whether a condition is True or False. The if statement is fundamental to creating dynamic, responsive programs that can adapt their behavior based on various conditions, user input, or data states.for
The for loop in Python is used to iterate over sequences like lists, tuples, strings, or any iterable object. Unlike traditional for loops in other languages that use counters, Python's for loop is more like a "for each" loop, automatically handling the iteration details. It's one of the most commonly used constructs in Python for processing collections and repeating actions.while
The while loop repeats a block of code as long as its condition remains True. Unlike a for loop, which iterates a known number of times over a sequence, a while loop is ideal when you don't know in advance how many iterations you'll need - like waiting for valid user input or running until some external condition changes. A while loop can also have an optional else clause that runs if the loop finishes normally, without hitting a break.break and continue
break and continue give you fine-grained control over loop execution. break immediately exits the nearest enclosing loop entirely, skipping any remaining iterations. continue skips just the rest of the current iteration and jumps straight to the next one, without exiting the loop. Both work inside for and while loops.
The if statement is Python's primary conditional control structure. It allows your program to make decisions by executing different code blocks based on whether a condition is True or False. The if statement is fundamental to creating dynamic, responsive programs that can adapt their behavior based on various conditions, user input, or data states.for
The for loop in Python is used to iterate over sequences like lists, tuples, strings, or any iterable object. Unlike traditional for loops in other languages that use counters, Python's for loop is more like a "for each" loop, automatically handling the iteration details. It's one of the most commonly used constructs in Python for processing collections and repeating actions.while
The while loop repeats a block of code as long as its condition remains True. Unlike a for loop, which iterates a known number of times over a sequence, a while loop is ideal when you don't know in advance how many iterations you'll need - like waiting for valid user input or running until some external condition changes. A while loop can also have an optional else clause that runs if the loop finishes normally, without hitting a break.break and continue
break and continue give you fine-grained control over loop execution. break immediately exits the nearest enclosing loop entirely, skipping any remaining iterations. continue skips just the rest of the current iteration and jumps straight to the next one, without exiting the loop. Both work inside for and while loops.