Back to Python
2025-12-265 min read

Python range()

Learn Python range() step by step with clear examples and exercises.

Title: Python range() Function: An In-Depth Guide with Real-World Examples and Common Mistakes

Why This Matters

The range() function is a fundamental building block in Python programming, enabling developers to generate sequences of numbers easily and efficiently. Understanding its usage can help you solve complex problems, write more readable code, and avoid common pitfalls during your coding journey. Whether you're preparing for an interview or working on a real-world project, mastering the range() function will prove invaluable.

Prerequisites

To follow this lesson, you should have a basic understanding of Python programming concepts such as variables, data types, control structures like loops (for and while loops), and conditional statements (if-else). If you're new to Python or need a refresher, consider reviewing our tutorials on these topics before proceeding.

Core Concept

The range() function in Python generates a sequence of numbers within a specified range. By default, the sequence starts at 0 and increments by 1 until it reaches one less than the specified end value. The syntax for using the range() function is as follows:

range(stop)

For example:

numbers = list(range(4))
print(numbers) # Output: [0, 1, 2, 3]

In the above example, the range() function generates a sequence of numbers from 0 to 3 (excluding 4), and we convert the generated sequence into a list for easier printing.

You can also specify the starting value and step size using the following syntax:

range(start, stop, step)

For example:

numbers = list(range(2, 6, 2))
print(numbers) # Output: [2, 4]

In this case, the range() function generates a sequence of numbers starting from 2 and incrementing by 2 until it reaches 6 (excluding 6).

The range() function is often used in for loops to iterate over a specific range of numbers:

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

Output:

0
1
2
3
4

Understanding the Step Argument

The step argument allows you to control the increment or decrement of the sequence. If the step is positive, the sequence will increase; if it's negative, the sequence will decrease. For example:

numbers = list(range(5, -1, -1))
print(numbers) # Output: [4, 3, 2, 1]

In this case, we generate a sequence of numbers that decreases from 5 to 1.

Using Floating-Point Values with the range() Function

You can also use floating-point values with the range() function to generate sequences with decimal increments:

numbers = list(range(0.5, 2.5, 0.5))
print(numbers) # Output: [0.5, 1.0, 1.5]

Using the range() Function with List Comprehensions

You can also use the range() function in list comprehensions to generate and manipulate sequences of numbers:

even_numbers = [num * 2 for num in range(10)]
print(even_numbers) # Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

In this example, we use a list comprehension to generate an even number sequence from 0 to 18.

Worked Example

Let's create a simple program that prints the Fibonacci sequence up to the 20th term using the range() function:

fibonacci = [0, 1]
for i in range(18):
next_number = fibonacci[-1] + fibonacci[-2]
fibonacci.append(next_number)
print(fibonacci[:20])

In this example, we use a for loop to iterate over 18 terms of the Fibonacci sequence using the range() function. We calculate each term by adding the previous two terms and append it to the fibonacci list. Finally, we print the first 20 terms of the generated Fibonacci sequence.

Common Mistakes

  1. Forgetting to convert the range object into a list or iterable:
numbers = range(4)
print(numbers) # Output: <range object at 0x7f8965d3b270>

To fix this, you can convert the range object into a list or iterate over it directly using a for loop.

  1. Assuming that the range() function includes the stop value:
numbers = list(range(5))
print(numbers) # Output: [0, 1, 2, 3, 4]

In this case, the range() function generates numbers from 0 to 4 (including 5), and we print the generated sequence as a list. To exclude the stop value in the output, you can use the slice notation:

numbers = list(range(5))
print(numbers[:-1]) # Output: [0, 1, 2, 3]

Common Mistakes (Continued)

  1. Misunderstanding the step argument:
numbers = list(range(5, 1, -1))
print(numbers) # Output: [5]

In this case, we expected the range() function to generate numbers from 5 down to 1, but since the step is -1 and the start value is greater than the stop value, it only generates one number. To fix this, you can change the start value or the step argument.

Practice Questions

  1. Write a program that prints the first 10 even numbers using the range() function.
  2. Write a program that generates all prime numbers between 1 and 50 using the range() function.
  3. Write a program that calculates the factorial of a given number (e.g., 5!) using the range() function.
  4. Write a program that finds the sum of all multiples of 7 between 1 and 100 using the range() function.

Practice Questions (Continued)

  1. Write a program that generates the first 10 Fibonacci numbers using the range() function and list comprehension.
  2. Write a program that generates all even perfect squares between 1 and 100 using the range() function and conditional statements.
  3. Write a program that generates the sequence of odd numbers that are multiples of 5 or 7 but not both using the range() function, conditional statements, and set operations.
  4. Write a program that finds the number of prime numbers between 1 and 100 using the range() function and list comprehension.

FAQ

What is the default step size for the range() function?

The default step size for the range() function is 1, meaning it increments by 1 by default.

Can I use negative numbers with the range() function?

Yes, you can specify negative numbers to generate a sequence of decreasing numbers using the range() function. For example:

numbers = list(range(5, -1, -1))
print(numbers) # Output: [4, 3, 2, 1]

Can I use floating-point values with the range() function?

Yes, you can use floating-point values with the range() function to generate sequences with decimal increments. For example:

numbers = list(range(0.5, 2.5, 0.5))
print(numbers) # Output: [0.5, 1.0, 1.5]

How can I include the stop value in the output of the range() function?

To include the stop value in the output of the range() function, you can use the slice notation:

numbers = list(range(5))
print(numbers[:]) # Output: [0, 1, 2, 3, 4]

How can I generate a sequence of numbers with a custom increment using the range() function?

To generate a sequence of numbers with a custom increment, you can use the range() function with a non-default step size:

numbers = list(range(0, 10, 2))
print(numbers) # Output: [0, 2, 4, 6, 8]
Python range() | Python | XQA Learn