Learnitweb

Python Range Data Type

This tutorial explains the Python range data type in a detailed, step-by-step format. It covers what a range is, how to create range objects, how many forms are available, how indexing and slicing work, the role of step values for increments and decrements, the immutability of range objects, and several illustrative examples. The format follows the same structure as your earlier Python data type tutorials.


1. Introduction to the Range Data Type

The range data type in Python is used to represent a sequence of numbers. Whenever you need to generate or work with a series of numeric values—such as 0 to 9, or 10 to 20, or numbers with a specific increment like 1, 3, 5, 7—Python provides the built-in range type.

The most common purpose of a range is:

• To represent a sequence of integers
• To use in loops (especially for loops)
• To generate predictable numerical sequences

A range object does not store all numbers in memory at once. Instead, it computes each number on demand, which makes it extremely memory-efficient.


2. Creating a Range Object

To create a range object, the syntax used is:

range(...)

Python provides three forms of the range constructor:

  1. range(stop)
  2. range(start, stop)
  3. range(start, stop, step)

Each form represents a different use case. Let us understand them in detail.


3. Form 1: range(stop)

This is the simplest form with one argument.

r = range(10)

Meaning:

• Start value is automatically 0
• Stop value is 10
• Step value is 1
• It represents values from 0 to stop - 1, i.e., 0 to 9

Example:

r = range(10)
print(type(r))       # <class 'range'>
print(r)             # range(0, 10)

for x in r:
    print(x)

Output:

0
1
2
3
4
5
6
7
8
9

Therefore:

range(n) gives numbers from 0 to n-1

4. Form 2: range(start, stop)

This form uses two arguments.

r = range(1, 10)

Meaning:

• Start value = 1
• Stop value = 10
• Step value = 1 (default)
• Represents numbers from start to stop - 1
• So this gives values 1 to 9

Example:

r = range(1, 10)

for x in r:
    print(x)

Output:

1
2
3
4
5
6
7
8
9

If you want numbers from 1 to 10 (inclusive), you must write:

range(1, 11)

Because the stop value is always excluded.


5. Form 3: range(start, stop, step)

This is the most powerful form with three arguments.

Syntax:

range(start, stop, step)

Here:

start = beginning number
stop = ending limit (excluded)
step = increment or decrement value

5.1 Incrementing Sequences

Example with step = 1:

r = range(1, 21, 1)

Represents:

1, 2, 3, ... , 20

Example with step = 2:

r = range(1, 21, 2)

Values:

1, 3, 5, 7, 9, 11, 13, 15, 17, 19

Example with step = 3:

r = range(1, 21, 3)

Values:

1, 4, 7, 10, 13, 16, 19

5.2 Decrementing Sequences

A negative step enables reverse counting.

Example:

r = range(20, 0, -5)

Represents:

20, 15, 10, 5

Another example:

r = range(22, 1, -5)

Values:

22, 17, 12, 7, 2

Thus, step can be positive (increment) or negative (decrement).


6. Number of Arguments in Each Form

• Form 1: range(stop) → one argument
• Form 2: range(start, stop) → two arguments
• Form 3: range(start, stop, step) → three arguments

Understanding these forms is fundamental because many Python loop operations depend on them.


7. Why Range is a Sequence

The term “sequence” means:

• It has a fixed order
• It supports indexing
• It supports slicing

Because range represents a sequence of numbers, both indexing and slicing operations are allowed.


8. Indexing in Range

Example:

r = range(10, 21)

This represents values:

10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

Accessing elements using index:

print(r[0])      # 10
print(r[-1])     # 20
print(r[3])      # 13

Just like lists, negative indexing works in ranges.


9. Slicing in Range

A slice operation on a range returns another range object, not a list.

Example:

r = range(10, 21)
r1 = r[1:5]

r[1:5] means values from index 1 to index 4.

Original range:

10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

Slice:

11, 12, 13, 14

Demonstration:

for x in r1:
    print(x)

Output:

11
12
13
14

Slicing works exactly like lists, but is optimized for range.


10. Immutability of Range Objects

Although range supports indexing and slicing, it is immutable.

Once created, you cannot change elements.

Example:

r = range(10, 21)
r[1] = 1000

This gives:

TypeError: 'range' object does not support item assignment

Reason:

• A range represents a mathematically fixed sequence
• It cannot be modified
• Only recreating a new range is possible


11. Why Range is Memory Efficient

Unlike lists, range does not store all numbers in memory.

Example:

range(1_000_000)

This represents ten lakh numbers but only consumes a few bytes because:

• It stores start, stop and step
• Each number is computed when needed

This efficiency is why range is heavily used in loops.


12. Summary of Key Points

• Use range when you want to represent a sequence of numbers in Python.
• Three forms exist:

  • range(stop)
  • range(start, stop)
  • range(start, stop, step)
    • Step value can be positive (increment) or negative (decrement).
    • Range represents values from start to stop−1.
    • Range objects are immutable.
    • Indexing and slicing are allowed.
    • Range is memory-efficient and widely used in loops.
    • Very commonly used in Python beginners’ programs and even in certification exams.