TOPIC 6: Python range ( )

Site: Learning Management System
Course: Edo College ICT Club Programme
Book: TOPIC 6: Python range ( )
Printed by: Guest user
Date: Wednesday, 26 August 2026, 7:07 AM

1. Introduction

The built-in range() function is used to generate a sequence of numbers. It is commonly used with for loops when you want to repeat an action a specific number of times.

The sequence of numbers created by range() has its own data type called range.

Note: A range is immutable, which means it cannot be changed after it has been created.

Creating Ranges

The range() function can be called with one, two, or three arguments.

Syntax:

range(start, stop, step)

Where:

  • start: The number to begin from (optional).

  • stop: The number where the range stops (required).

  • step: The difference between each number (optional).

Calling range() with One Argument

If only one argument is provided, Python treats it as the stop value.

The starting value automatically becomes 0.

The stop value is not included in the sequence.

Example:

x = range(10)

print(list(x))

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Notice that 10 is not included.

2. Calling range() with Two Arguments

When two arguments are provided:

  • The first argument is the start value.

  • The second argument is the stop value.

Example:

x = range(3, 10)

print(list(x))

Output:

[3, 4, 5, 6, 7, 8, 9]

The sequence starts at 3 and stops before 10.

3. Calling range() with Three Arguments

When three arguments are provided:

  • The first argument is the start value.

  • The second argument is the stop value.

  • The third argument is called the step value.

The step value determines how much each number increases.

If the step value is not provided, Python uses 1 by default.

Example:

x = range(3, 10, 2)

print(list(x))

Output:

[3, 5, 7, 9]

The numbers increase by 2 each time.

4. Using range() in a for Loop

One of the most common uses of range() is with a for loop.

Example:

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

Output:

0
1
2
3
4
5
6
7
8
9

The loop runs once for every number in the range.

5. Displaying a Range

A range object is not displayed directly as a normal list.

To display all the values, convert it into a list using the list() function.

Example:

print(list(range(5)))

Output:

[0, 1, 2, 3, 4]

Example:

print(list(range(1, 6)))

Output:

[1, 2, 3, 4, 5]

Example:

print(list(range(5, 20, 3)))

Output:

[5, 8, 11, 14, 17]

6. Accessing Values in a Range & Slicing a Range

Accessing Values in a Range

Just like lists, you can access values in a range using their index.

Example:

r = range(10)

print(r[2])

Output:

2

Since indexing starts at 0, index 2 contains the value 2.

Slicing a Range

You can also slice a range to get part of the sequence.

Example:

r = range(10)

print(r[:3])

Output:

range(0, 3)

This creates a new range containing the first three values.

7. Membership Testing & Finding the Length of a Range

Membership Testing

You can check whether a number exists in a range using the in operator.

Example:

r = range(0, 10, 2)

print(6 in r)
print(7 in r)

Output:

True
False

Explanation:

  • 6 is in the range.

  • 7 is not in the range because the numbers increase by 2.

Finding the Length of a Range

Use the len() function to determine how many values are in a range.

Example:

r = range(0, 10, 2)

print(len(r))

Output:

5

The range contains:

0
2
4
6
8

So the length is 5.

8. Summary

  • range() creates a sequence of numbers.

  • It is commonly used with for loops.

  • range(stop) starts from 0.

  • range(start, stop) starts from the specified value.

  • range(start, stop, step) allows you to control the interval between numbers.

  • The stop value is not included.

  • Use list() to display all the values in a range.

  • Use len() to count the number of values in a range.

  • Use the in operator to check if a value exists in a range.

9. Class Exercise

Exercise 1

Create a range from 0 to 9 and display it as a list.

Exercise 2

Create a range starting from 5 and ending before 15.

Display the result as a list.

Exercise 3

Create a range from 2 to 20 with a step value of 3.

Display the result.

Exercise 4

Write a program that prints the numbers from 1 to 10 using a for loop and the range() function.

Exercise 5

What will be the output of the following code?

print(list(range(3)))

Exercise 6

What is the output?

r = range(0, 10, 2)

print(8 in r)
print(9 in r)

Exercise 7

Find the length of the following range.

r = range(2, 12, 2)

print(len(r))