No counters, no conditions, no increments โ Python's for loop walks a sequence directly. Here's range() in all its forms, plus looping over strings, lists, tuples, and sets.
This topic covers Python's for loop โ how it differs from the traditional C-style for loop, how the range() function works (including its variations), and how for loops behave when iterating over different sequence types (list, tuple, set, string).
for loop iterates directly over a sequence of values โ there's no manual counter/condition/increment syntax like in C.range() function is commonly used to generate a sequence of numbers to loop over.for loop in Python can iterate over any iterable: lists, tuples, sets, strings, and more.In C-like languages, a for loop looks like this:
for (i = 0; i < 10; i++) {
code
}
This explicitly defines:
i = 0i < 10i++Python does not use this style. Instead, Python's for loop iterates over items in a sequence:
for i in sequence:
code
Note: Python hides the "counting" mechanics behind iterables like range() โ you don't manage the index yourself.
range() generates a sequence of numbers. It doesn't display values directly โ wrap it in list() to see the actual values, or use it directly in a for loop.
Init signature: range(self, /, *args, **kwargs)
Docstring:
range(stop) -> range object
range(start, stop[, step]) -> range object
So range() can be called in 3 ways:
| Form | Example | Meaning |
|---|---|---|
range(stop) | range(5) | Numbers from 0 to stop-1 |
range(start, stop) | range(1, 11) | Numbers from start to stop-1 |
range(start, stop, step) | range(1, 11, 2) | Numbers from start to stop-1, incrementing by step |
list(range(5))
# Output: [0, 1, 2, 3, 4]
โ ๏ธ Only one argument (stop) โ starts at0by default, stops before the given number.
list(range(1, 11))
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Two arguments โstartandstop(stop is exclusive, so it goes up to10, not11).
list(range(15))
# Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
list(range(1, 11, 2))
# Output: [1, 3, 5, 7, 9]
Three arguments โstart,stop,step. Here step =2, so it skips every other number. Note11is excluded (stop is exclusive) and9is the last odd number before it.
list(range(10, 0, -1))
# Output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Negative step counts backwards. Start =10, stop =0(exclusive, so0itself isn't included), step =-1.
๐ก Tip: When the step is negative, start must be greater than stop, otherwise the range will be empty.
Once you understand range(), using it in a loop is straightforward:
for i in range(10, 0, -1):
print(i)
Output:
10
9
8
7
6
5
4
3
2
1
This prints a countdown from 10 to 1.
A for loop isn't limited to range() โ it works with any sequence/iterable.
for i in "Kolkata":
print(i)
Output:
K
o
l
k
a
t
a
Strings are iterated character by character.
for i in [1, 2, 3, 5]:
print(i)
Output:
1
2
3
5
for i in (1, 2, 3, 5):
print(i)
Output:
1
2
3
5
for i in {1, 2, 3, 5}:
print(i)
Output:
1
2
3
5
"Kolkata" # a string
["Kolkata", "Delhi", "Mumbai"] # a list
("Kolkata", "Delhi", "Mumbai") # a tuple
These were shown just before the loop examples as a refresher on sequence data types (string, list, tuple) that can be looped over.
# Basic range forms
range(stop) # 0 to stop-1
range(start, stop) # start to stop-1
range(start, stop, step) # start to stop-1, incrementing/decrementing by step
# See actual values
list(range(...))
# For loop over range
for i in range(...):
print(i)
# For loop over any sequence
for i in some_sequence:
print(i)
range() is exclusive of the stop value โ it never includes the stop number itself.start is 0 and default step is 1 when not specified.step requires start > stop to produce a descending sequence.for loop works uniformly across strings, lists, tuples, and sets โ no special syntax needed per type.range() itself returns a range object, not a list โ use list() to materialize it for viewing/debugging.for loop = "for each item in this sequence" (not counter-based like C).range(stop), range(start, stop), range(start, stop, step) โ three flavors, memorize the exclusivity of stop.start must be bigger than stop.Python's for loop iterates directly over sequences instead of using an explicit counter/condition/increment like C. The range() function โ with 1, 2, or 3 arguments (stop, start/stop, or start/stop/step) โ is the most common way to generate number sequences to loop over, and it always excludes the stop value. A negative step lets you count backwards, provided start > stop. Beyond range(), a for loop works the same way on strings (character by character), lists, tuples, and sets โ though sets don't guarantee order, so don't depend on it.