Generating Sequences with range()

๐Ÿท๏ธ Loops and Iteration / The For Loop


๐Ÿง  Context Introduction

When working with loops, you often need to repeat an action a specific number of times or step through a sequence of numbers. Python's range() function is your go-to tool for generating these number sequences efficiently. Instead of manually creating lists of numbers, range() produces them on the fly, saving memory and making your code cleaner.


โš™๏ธ What is range()?

  • range() is a built-in Python function that generates a sequence of numbers.
  • It is commonly used with for loops to control how many times the loop runs.
  • The numbers are generated lazily โ€” meaning they are produced one at a time as needed, not stored all at once in memory.

๐Ÿ› ๏ธ Three Ways to Use range()

1. Single argument โ€” range(stop)

  • Generates numbers from 0 up to (but not including) the stop value.
  • Example: range(5) produces the sequence: 0, 1, 2, 3, 4

2. Two arguments โ€” range(start, stop)

  • Generates numbers from start up to (but not including) stop.
  • Example: range(2, 7) produces the sequence: 2, 3, 4, 5, 6

3. Three arguments โ€” range(start, stop, step)

  • Generates numbers from start up to (but not including) stop, incrementing by step.
  • Example: range(1, 10, 2) produces the sequence: 1, 3, 5, 7, 9

๐Ÿ“Š Comparison Table: range() Variations

Form Example Output Sequence Use Case
range(stop) range(4) 0, 1, 2, 3 Loop a fixed number of times starting from 0
range(start, stop) range(3, 8) 3, 4, 5, 6, 7 Loop from a specific starting point
range(start, stop, step) range(0, 10, 3) 0, 3, 6, 9 Skip numbers or count backwards

๐Ÿ•ต๏ธ Important Details to Remember

  • The stop value is always exclusive โ€” the sequence stops before reaching it.
  • The step value can be negative to generate descending sequences.
  • Example: range(10, 0, -2) produces: 10, 8, 6, 4, 2
  • All arguments must be integers (whole numbers).
  • range() works best with for loops, but you can also convert it to a list using list(range(...)) if you need to see all values.

๐Ÿงช Practical Examples in Action

Example 1: Simple count from 0 to 4

  • Use range(5) inside a for loop.
  • The loop body runs 5 times, with the variable taking values 0, 1, 2, 3, 4.

Example 2: Count from 5 to 10

  • Use range(5, 11).
  • The loop runs 6 times with values 5, 6, 7, 8, 9, 10.

Example 3: Count by 5s from 0 to 50

  • Use range(0, 51, 5).
  • The loop runs 11 times with values 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50.

Example 4: Count down from 10 to 1

  • Use range(10, 0, -1).
  • The loop runs 10 times with values 10, 9, 8, 7, 6, 5, 4, 3, 2, 1.

๐Ÿ’ก Common Mistakes to Avoid

  • Forgetting that stop is exclusive โ€” range(3) gives 0, 1, 2, not 0, 1, 2, 3.
  • Using a step of 0 โ€” this causes an error because you cannot step by zero.
  • Using non-integer values โ€” range(1.5, 5.5) will raise a TypeError.
  • Assuming range() returns a list โ€” it returns a range object, which is memory efficient but needs to be converted if you need a list.

โœ… Quick Summary

  • range() generates integer sequences for loops.
  • Use range(stop) for simple 0-based counting.
  • Use range(start, stop) for custom starting points.
  • Use range(start, stop, step) for custom increments or decrements.
  • Remember: stop is exclusive, all arguments must be integers.

With range() in your toolkit, you can precisely control how many times your loops execute and what values they work with โ€” a fundamental skill for writing efficient and readable Python code.


The range() function generates a sequence of numbers that you can use to control how many times a loop runs or to create lists of numbers.


๐ŸŸข Example 1: Basic range with one argument

This example shows how range() generates numbers from 0 up to (but not including) the specified number.

numbers = range(5)
for number in numbers:
    print(number)

๐Ÿ“ค Output: 0 1 2 3 4


๐Ÿ”ต Example 2: Range with start and stop arguments

This example shows how to specify both the starting number and the stopping number.

numbers = range(2, 7)
for number in numbers:
    print(number)

๐Ÿ“ค Output: 2 3 4 5 6


๐ŸŸก Example 3: Range with step argument

This example shows how to skip numbers by adding a step value.

numbers = range(0, 20, 5)
for number in numbers:
    print(number)

๐Ÿ“ค Output: 0 5 10 15


๐ŸŸ  Example 4: Counting backwards with a negative step

This example shows how to generate a decreasing sequence of numbers.

numbers = range(10, 0, -2)
for number in numbers:
    print(number)

๐Ÿ“ค Output: 10 8 6 4 2


๐Ÿ”ด Example 5: Converting range to a list for inspection

This example shows how to see all the numbers in a range by converting it to a list.

even_numbers = range(2, 11, 2)
number_list = list(even_numbers)
print(number_list)

๐Ÿ“ค Output: [2, 4, 6, 8, 10]


๐ŸŸฃ Example 6: Using range to repeat an action a fixed number of times

This example shows how to use range when you don't need the actual numbers, just the count.

for iteration in range(3):
    print("System check complete")

๐Ÿ“ค Output: System check complete System check complete System check complete


โšช Example 7: Practical use โ€” generating index numbers for a list

This example shows how engineers use range to access list items by their position.

sensor_readings = [23.5, 24.1, 22.8, 25.0]
for index in range(len(sensor_readings)):
    print(f"Sensor {index + 1}: {sensor_readings[index]}")

๐Ÿ“ค Output: Sensor 1: 23.5 Sensor 2: 24.1 Sensor 3: 22.8 Sensor 4: 25.0


Comparison Table

Function Call Generated Sequence Use Case
range(5) 0, 1, 2, 3, 4 Repeat an action 5 times
range(2, 7) 2, 3, 4, 5, 6 Start at a specific number
range(0, 20, 5) 0, 5, 10, 15 Skip values by a fixed step
range(10, 0, -2) 10, 8, 6, 4, 2 Count backwards
range(2, 11, 2) 2, 4, 6, 8, 10 Generate only even numbers

๐Ÿง  Context Introduction

When working with loops, you often need to repeat an action a specific number of times or step through a sequence of numbers. Python's range() function is your go-to tool for generating these number sequences efficiently. Instead of manually creating lists of numbers, range() produces them on the fly, saving memory and making your code cleaner.


โš™๏ธ What is range()?

  • range() is a built-in Python function that generates a sequence of numbers.
  • It is commonly used with for loops to control how many times the loop runs.
  • The numbers are generated lazily โ€” meaning they are produced one at a time as needed, not stored all at once in memory.

๐Ÿ› ๏ธ Three Ways to Use range()

1. Single argument โ€” range(stop)

  • Generates numbers from 0 up to (but not including) the stop value.
  • Example: range(5) produces the sequence: 0, 1, 2, 3, 4

2. Two arguments โ€” range(start, stop)

  • Generates numbers from start up to (but not including) stop.
  • Example: range(2, 7) produces the sequence: 2, 3, 4, 5, 6

3. Three arguments โ€” range(start, stop, step)

  • Generates numbers from start up to (but not including) stop, incrementing by step.
  • Example: range(1, 10, 2) produces the sequence: 1, 3, 5, 7, 9

๐Ÿ“Š Comparison Table: range() Variations

Form Example Output Sequence Use Case
range(stop) range(4) 0, 1, 2, 3 Loop a fixed number of times starting from 0
range(start, stop) range(3, 8) 3, 4, 5, 6, 7 Loop from a specific starting point
range(start, stop, step) range(0, 10, 3) 0, 3, 6, 9 Skip numbers or count backwards

๐Ÿ•ต๏ธ Important Details to Remember

  • The stop value is always exclusive โ€” the sequence stops before reaching it.
  • The step value can be negative to generate descending sequences.
  • Example: range(10, 0, -2) produces: 10, 8, 6, 4, 2
  • All arguments must be integers (whole numbers).
  • range() works best with for loops, but you can also convert it to a list using list(range(...)) if you need to see all values.

๐Ÿงช Practical Examples in Action

Example 1: Simple count from 0 to 4

  • Use range(5) inside a for loop.
  • The loop body runs 5 times, with the variable taking values 0, 1, 2, 3, 4.

Example 2: Count from 5 to 10

  • Use range(5, 11).
  • The loop runs 6 times with values 5, 6, 7, 8, 9, 10.

Example 3: Count by 5s from 0 to 50

  • Use range(0, 51, 5).
  • The loop runs 11 times with values 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50.

Example 4: Count down from 10 to 1

  • Use range(10, 0, -1).
  • The loop runs 10 times with values 10, 9, 8, 7, 6, 5, 4, 3, 2, 1.

๐Ÿ’ก Common Mistakes to Avoid

  • Forgetting that stop is exclusive โ€” range(3) gives 0, 1, 2, not 0, 1, 2, 3.
  • Using a step of 0 โ€” this causes an error because you cannot step by zero.
  • Using non-integer values โ€” range(1.5, 5.5) will raise a TypeError.
  • Assuming range() returns a list โ€” it returns a range object, which is memory efficient but needs to be converted if you need a list.

โœ… Quick Summary

  • range() generates integer sequences for loops.
  • Use range(stop) for simple 0-based counting.
  • Use range(start, stop) for custom starting points.
  • Use range(start, stop, step) for custom increments or decrements.
  • Remember: stop is exclusive, all arguments must be integers.

With range() in your toolkit, you can precisely control how many times your loops execute and what values they work with โ€” a fundamental skill for writing efficient and readable Python code.

Interactive Views

You are currently in ๐Ÿ“š All-in-One mode. Use the tabs at the top to switch to ๐Ÿ“– Theory Only or ๐Ÿ’ป Code Only views.

The range() function generates a sequence of numbers that you can use to control how many times a loop runs or to create lists of numbers.


๐ŸŸข Example 1: Basic range with one argument

This example shows how range() generates numbers from 0 up to (but not including) the specified number.

numbers = range(5)
for number in numbers:
    print(number)

๐Ÿ“ค Output: 0 1 2 3 4


๐Ÿ”ต Example 2: Range with start and stop arguments

This example shows how to specify both the starting number and the stopping number.

numbers = range(2, 7)
for number in numbers:
    print(number)

๐Ÿ“ค Output: 2 3 4 5 6


๐ŸŸก Example 3: Range with step argument

This example shows how to skip numbers by adding a step value.

numbers = range(0, 20, 5)
for number in numbers:
    print(number)

๐Ÿ“ค Output: 0 5 10 15


๐ŸŸ  Example 4: Counting backwards with a negative step

This example shows how to generate a decreasing sequence of numbers.

numbers = range(10, 0, -2)
for number in numbers:
    print(number)

๐Ÿ“ค Output: 10 8 6 4 2


๐Ÿ”ด Example 5: Converting range to a list for inspection

This example shows how to see all the numbers in a range by converting it to a list.

even_numbers = range(2, 11, 2)
number_list = list(even_numbers)
print(number_list)

๐Ÿ“ค Output: [2, 4, 6, 8, 10]


๐ŸŸฃ Example 6: Using range to repeat an action a fixed number of times

This example shows how to use range when you don't need the actual numbers, just the count.

for iteration in range(3):
    print("System check complete")

๐Ÿ“ค Output: System check complete System check complete System check complete


โšช Example 7: Practical use โ€” generating index numbers for a list

This example shows how engineers use range to access list items by their position.

sensor_readings = [23.5, 24.1, 22.8, 25.0]
for index in range(len(sensor_readings)):
    print(f"Sensor {index + 1}: {sensor_readings[index]}")

๐Ÿ“ค Output: Sensor 1: 23.5 Sensor 2: 24.1 Sensor 3: 22.8 Sensor 4: 25.0


Comparison Table

Function Call Generated Sequence Use Case
range(5) 0, 1, 2, 3, 4 Repeat an action 5 times
range(2, 7) 2, 3, 4, 5, 6 Start at a specific number
range(0, 20, 5) 0, 5, 10, 15 Skip values by a fixed step
range(10, 0, -2) 10, 8, 6, 4, 2 Count backwards
range(2, 11, 2) 2, 4, 6, 8, 10 Generate only even numbers