Python range() Function: Syntax, Examples & Practice Programs

Python range() Function

What is the purpose of range()?

The range() function in Python is used to generate a sequence of numbers according to the values specified by the programmer.

For example:

range(5)

represents the sequence:

0, 1, 2, 3, 4

Notice that 5 is not included.

The range() function is commonly used when a program needs a sequence of numbers. Its use with for loops can be discussed separately later.

Syntax of range()

Python provides three forms of the range() function:

range(stop)
range(start, stop)
range(start, stop, step)

Where:

Argument Meaning
start The number from which the sequence starts
stop The number at which the sequence stops (not included)
step The difference between consecutive numbers

1. range(stop)

When only one value is given, it is treated as the stop value.

range(5)

It represents:

0, 1, 2, 3, 4

So:

range(5)

generates numbers starting from 0 and ending just before 5.

Therefore:

range(5) → 0, 1, 2, 3, 4

2. range(start, stop)

Two values can be specified:

range(2, 7)

Here:

  • start = 2
  • stop = 7

The sequence is:

2, 3, 4, 5, 6

Again, the stop value 7 is not included.

3. range(start, stop, step)

Three values can be specified:

range(2, 10, 2)

Here:

  • start = 2
  • stop = 10
  • step = 2

The sequence is:

2, 4, 6, 8

The numbers increase by 2 each time.

Important Point

The stop value in range() is always excluded.

For example:

range(1, 6)

represents:

1, 2, 3, 4, 5

not:

1, 2, 3, 4, 5, 6

A Small Experiment

You can use list() to see the numbers represented by a range object:

numbers = range(5)
print(list(numbers))

Output:

[0, 1, 2, 3, 4]

Here, range(5) creates a range object, and list() converts that sequence into a list so that we can easily see its values.

More Examples

print(list(range(0, 5)))
Output: [0, 1, 2, 3, 4]
print(list(range(2, 10)))
Output: [2, 3, 4, 5, 6, 7, 8, 9]
print(list(range(3, 50, 4)))
Output: [3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47]
Note: In this post, we are learning the range() function itself. Its use with for loops will be covered separately.

Practice Programs: Negative step Values

The following programs demonstrate descending sequences, negative step values, and zero or negative arguments.

1. Descending sequence

print(list(range(5, 0, -1)))

Output: [5, 4, 3, 2, 1]

2. Negative step of 2

print(list(range(10, 1, -2)))

Output: [10, 8, 6, 4, 2]

3. Starting from zero

print(list(range(0, -5, -1)))

Output: [0, -1, -2, -3, -4]

4. Negative start and stop

print(list(range(-1, -10, -2)))

Output: [-1, -3, -5, -7, -9]

5. Zero step — what happens?

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

Output: ValueError: range() arg 3 must not be zero

Important: A negative step makes the sequence decrease. A zero step is not allowed and produces a ValueError.

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »