0 to Infinity Lesson

Python Loops Using range()

Learn counting, reverse counting, steps, tables, sums, indexes, nested loops, and pattern printing using one of the most important tools in Python.

1. What is this lesson?

This is a complete 0 to infinity lesson on Python loops using range(). We begin from counting 0, 1, 2, 3 and slowly reach tables, sums, reverse counting, patterns, nested loops, lists, indexes, and mini projects.

0 level

Understand what repetition means and why a loop saves time.

Core level

Master range(stop), range(start, stop), and range(start, stop, step).

Infinity level

Build tables, patterns, search logic, totals, indexes, and nested loop programs.

Big idea: range() gives numbers one by one. The for loop uses those numbers to repeat work.

2. The mental model of range

Think of range() as a number machine. It does not print by itself. It gives numbers to the loop one at a time.

Expression Numbers produced Meaning
range(5) 0, 1, 2, 3, 4 Start from 0. Stop before 5.
range(1, 6) 1, 2, 3, 4, 5 Start from 1. Stop before 6.
range(2, 11, 2) 2, 4, 6, 8, 10 Jump by 2.
range(10, 0, -1) 10, 9, 8, ..., 1 Reverse counting.
Most important rule: the stop value is not included. range(1, 6) stops before 6, so it gives 1 to 5.
Advertisement

3. Your first range loop

This loop repeats the print() statement five times.

for i in range(5):
    print(i)

Output:

0
1
2
3
4

The variable i receives a new value in every round.

Round Value of i Printed
1 0 0
2 1 1
3 2 2
4 3 3
5 4 4

4. range(start, stop)

When you want to control the first number, give two values.

for i in range(1, 6):
    print(i)

Output:

1
2
3
4
5
Read range(1, 6) as: start at 1, go up to 6, but do not include 6.

Print numbers from 10 to 15

for i in range(10, 16):
    print(i)

5. range(start, stop, step)

The third value is the jump size.

Even numbers from 2 to 20

for i in range(2, 21, 2):
    print(i)

Odd numbers from 1 to 19

for i in range(1, 20, 2):
    print(i)

Multiples of 5

for i in range(5, 51, 5):
    print(i)
Formula: range(start, stop, step) means start here, stop before this, and move by this step.

6. Reverse counting with negative step

To go backward, the step must be negative.

for i in range(10, 0, -1):
    print(i)

Output:

10
9
8
7
6
5
4
3
2
1

Common mistake

# This prints nothing because Python cannot move from 10 to 1 using +1
for i in range(10, 1):
    print(i)
When start is bigger than stop, use a negative step.

7. Multiplication tables

Tables are one of the best ways to understand loops using range.

Table of 7

for i in range(1, 11):
    print("7 x", i, "=", 7 * i)

Any table using a variable

n = 12

for i in range(1, 11):
    print(n, "x", i, "=", n * i)

Tables from 2 to 5

for table in range(2, 6):
    print("Table of", table)

    for i in range(1, 11):
        print(table, "x", i, "=", table * i)

    print()

8. Total, count, and average

A loop can do more than print. It can build an answer step by step.

Sum of numbers from 1 to 10

total = 0

for i in range(1, 11):
    total = total + i

print("Total:", total)

Count numbers divisible by 3

count = 0

for i in range(1, 51):
    if i % 3 == 0:
        count = count + 1

print("Numbers divisible by 3:", count)

Average of first 10 natural numbers

total = 0
count = 0

for i in range(1, 11):
    total = total + i
    count = count + 1

average = total / count
print("Average:", average)

9. Using range with indexes

Sometimes we need positions: 0, 1, 2, 3. These positions are called indexes.

names = ["Aarav", "Siya", "Mohan", "Riya"]

for i in range(len(names)):
    print(i, names[i])

Output:

0 Aarav
1 Siya
2 Mohan
3 Riya

Print serial numbers starting from 1

names = ["Aarav", "Siya", "Mohan", "Riya"]

for i in range(len(names)):
    print(i + 1, names[i])
Use range(len(list_name)) when you need the index. Use for item in list_name when you need only the value.

10. Pattern printing using range

Pattern printing builds strong loop thinking because every row has a formula.

Left triangle

n = 5

for row in range(1, n + 1):
    print("*" * row)

Output:

*
**
***
****
*****

Right aligned triangle

n = 5

for row in range(1, n + 1):
    spaces = n - row
    stars = row
    print(" " * spaces + "*" * stars)

Output:

    *
   **
  ***
 ****
*****

Pattern chart

row spaces stars formula
1 4 1 spaces = n - row, stars = row
2 3 2 spaces = n - row, stars = row
3 2 3 spaces = n - row, stars = row
4 1 4 spaces = n - row, stars = row
5 0 5 spaces = n - row, stars = row

11. Nested loops

A nested loop is a loop inside another loop. The outer loop usually controls rows. The inner loop usually controls columns.

5 x 5 grid

for row in range(1, 6):
    for col in range(1, 6):
        print("*", end=" ")
    print()

Output:

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Coordinate grid

for row in range(1, 4):
    for col in range(1, 4):
        print("(", row, ",", col, ")", end=" ")
    print()

Output:

( 1 , 1 ) ( 1 , 2 ) ( 1 , 3 )
( 2 , 1 ) ( 2 , 2 ) ( 2 , 3 )
( 3 , 1 ) ( 3 , 2 ) ( 3 , 3 )

12. break and continue with range

break stops the loop. continue skips the current round.

Stop when number becomes 6

for i in range(1, 11):
    if i == 6:
        break
    print(i)

Skip number 6

for i in range(1, 11):
    if i == 6:
        continue
    print(i)
Use break when the work is finished. Use continue when only one value should be skipped.

13. Common mistakes

Mistake Why it happens Fix
range(1, 10) when you wanted 1 to 10 Stop is excluded. Use range(1, 11).
Forgetting colon A loop header needs :. for i in range(5):
Wrong indentation Python uses indentation for blocks. Keep loop body indented.
range(10, 1) prints nothing Default step is +1. Use range(10, 1, -1).
Using i after copying code blindly i is only a variable name. Use meaningful names like row, col, number.

14. Full practice notebook

Copy this full notebook into the Python editor and run it. Then change the numbers and observe the output.

"""
0 to Infinity: Loops using range()
Programmer's Picnic by Champak Roy
"""

print("=" * 60)
print("LOOPS USING RANGE")
print("=" * 60)

print("
1. range(stop)")
for i in range(5):
    print(i)

print("
2. range(start, stop)")
for i in range(1, 6):
    print(i)

print("
3. range(start, stop, step)")
for i in range(2, 21, 2):
    print(i)

print("
4. Reverse counting")
for i in range(10, 0, -1):
    print(i)

print("
5. Table of 7")
for i in range(1, 11):
    print("7 x", i, "=", 7 * i)

print("
6. Sum from 1 to 10")
total = 0
for i in range(1, 11):
    total = total + i
print("Total:", total)

print("
7. Index loop")
names = ["Aarav", "Siya", "Mohan", "Riya"]
for i in range(len(names)):
    print(i + 1, names[i])

print("
8. Right aligned triangle")
n = 5
for row in range(1, n + 1):
    spaces = n - row
    stars = row
    print(" " * spaces + "*" * stars)

print("
9. 5 x 5 grid")
for row in range(1, 6):
    for col in range(1, 6):
        print("*", end=" ")
    print()

print("
10. Coordinate grid")
for row in range(1, 4):
    for col in range(1, 4):
        print("(", row, ",", col, ")", end=" ")
    print()

print("
11. break")
for i in range(1, 11):
    if i == 6:
        break
    print(i)

print("
12. continue")
for i in range(1, 11):
    if i == 6:
        continue
    print(i)

print("
Lesson complete")

15. Python Editor

Run the examples here. Change range() values and see how the output changes.

Embedded Python EditorLoops using range

16. Practice tasks

1 Counting

Print numbers from 1 to 20.

2 Reverse

Print numbers from 20 to 1.

3 Even numbers

Print even numbers from 2 to 100.

4 Table

Print the multiplication table of 13.

5 Sum

Find the sum of numbers from 1 to 100.

6 Pattern

Print a right aligned triangle of height 7.

7 Grid

Print a 7 x 7 coordinate grid.

Show solved practice answers
# 1. Counting
for i in range(1, 21):
    print(i)

# 2. Reverse
for i in range(20, 0, -1):
    print(i)

# 3. Even numbers
for i in range(2, 101, 2):
    print(i)

# 4. Table of 13
for i in range(1, 11):
    print("13 x", i, "=", 13 * i)

# 5. Sum 1 to 100
total = 0
for i in range(1, 101):
    total = total + i
print(total)

# 6. Right aligned triangle
n = 7
for row in range(1, n + 1):
    spaces = n - row
    stars = row
    print(" " * spaces + "*" * stars)

# 7. 7 x 7 coordinate grid
for row in range(1, 8):
    for col in range(1, 8):
        print("(", row, ",", col, ")", end=" ")
    print()

17. Final mini project

Create a single program that prints a triangle, multiplication tables, and a 7 x 7 coordinate board.

n = 5

print("RIGHT ALIGNED TRIANGLE")
for row in range(1, n + 1):
    spaces = n - row
    stars = row
    print(" " * spaces + "*" * stars)

print("
MULTIPLICATION TABLES FROM 2 TO 5")
for table in range(2, 6):
    print("
Table of", table)
    for i in range(1, 11):
        print(table, "x", i, "=", table * i)

print("
7 x 7 COORDINATE BOARD")
for row in range(1, 8):
    for col in range(1, 8):
        print("(", row, ",", col, ")", end=" ")
    print()
This project uses simple loops, nested loops, range start-stop-step thinking, and pattern formulas.

18. What you now know

You understand range

You know range(stop), range(start, stop), and range(start, stop, step).

You can count forward and backward

You can use positive and negative steps.

You can calculate with loops

You can create totals, counts, averages, and tables.

You can use indexes

You can combine range() with len().

You can print patterns

You can build triangles and grids using row-column logic.

You are ready for DSA

Loops using range are the base of arrays, searching, sorting, matrices, and pattern problems.

Top