Python For Loops

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.

Overview

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).

Key Concepts

Detailed Explanation

1. C-style vs Python for loop

In C-like languages, a for loop looks like this:

for (i = 0; i < 10; i++) {
    code
}

This explicitly defines:

  1. Initialization โ€” i = 0
  2. Condition โ€” i < 10
  3. Increment โ€” i++

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.

2. The range() function

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.

Signature (from Jupyter's docstring popup)

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:

FormExampleMeaning
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

Examples observed

list(range(5))
# Output: [0, 1, 2, 3, 4]
โš ๏ธ Only one argument (stop) โ†’ starts at 0 by default, stops before the given number.
list(range(1, 11))
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Two arguments โ†’ start and stop (stop is exclusive, so it goes up to 10, not 11).
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. Note 11 is excluded (stop is exclusive) and 9 is 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, so 0 itself isn't included), step = -1.

๐Ÿ’ก Tip: When the step is negative, start must be greater than stop, otherwise the range will be empty.

3. Using for with range()

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.

4. Iterating over other sequence types

A for loop isn't limited to range() โ€” it works with any sequence/iterable.

a) Iterating over a String

for i in "Kolkata":
    print(i)

Output:

K
o
l
k
a
t
a

Strings are iterated character by character.

b) Iterating over a List

for i in [1, 2, 3, 5]:
    print(i)

Output:

1
2
3
5

c) Iterating over a Tuple

for i in (1, 2, 3, 5):
    print(i)

Output:

1
2
3
5

d) Iterating over a Set

for i in {1, 2, 3, 5}:
    print(i)

Output:

1
2
3
5
โš ๏ธ Important: Sets are unordered collections. In this particular example the printed order happened to match insertion order, but that is not guaranteed in general โ€” don't rely on sets preserving order.

Supplementary: Other sequence-type reminders seen in notebook

"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.

Commands / Syntax Summary

# 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)

Important Points

Things to Remember

Quick Revision

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.