Nearby lessons

26 of 108

Python - Range Data Type

Introduction

The range data type is used to represent a sequence of numbers.

It is mainly used in loops to generate a sequence of integers.

Features of range Data Type

  1. Represents a sequence of numbers.
  2. Immutable.
  3. Commonly used in loops.
  4. Memory efficient.
  5. Supports indexing.

Important Note

The elements of a range object cannot be modified.

This means the range data type is immutable.

Syntax of range()

Python provides three forms of the range() function.

Form 1 - range(stop)

This form generates numbers from 0 to stop - 1.

Example - range(stop)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
0
1
2
3
4
5
6
7
8
9

Explanation

range(10) generates numbers from 0 to 9.

Form 2 - range(start, stop)

This form generates numbers from start to stop - 1.

Example - range(start, stop)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
11
12
13
14
15
16
17
18
19

Explanation

range(10, 20) generates numbers from 10 to 19.

Form 3 - range(start, stop, step)

This form generates numbers using a step value.

The step value can be positive or negative.

Example - Positive Step

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
12
14
16
18

Explanation

Here, 2 is the increment value.

range(10, 20, 2) starts from 10, increases by 2, and stops before 20.

Example - Negative Step

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
9
8
7
6
5
4
3
2
1

Accessing range Elements Using Index

A range object supports indexing.

We can access elements using positive indexes.

Example - Indexing

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10

Invalid Index Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
IndexError: range object index out of range

range is Immutable

Elements of a range object cannot be modified after creation.

Example - Invalid Modification

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError: 'range' object does not support item assignment

Converting range to List

We can convert a range object into a list using the list() function.

Example - list() Function

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Using range in for Loop

The range() function is mainly used with for loops.

Example - Print Hello

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Hello
Hello
Hello
Hello
Hello

Example - Print Even Numbers

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
0
2
4
6
8
10

Example - Print Odd Numbers

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1
3
5
7
9

Length of range Object

Use the len() function to find the number of elements in a range object.

Example - len() Function

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10

Advantages of range

  1. Memory efficient.
  2. Faster iteration.
  3. Useful in loops.
  4. Generates numbers dynamically.

Difference Between List and range

List range
Stores all values in memory. Generates values dynamically.
Mutable. Immutable.
Uses more memory. Uses less memory.

Example - List

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[0, 1, 2, 3, 4]

Example - range

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
range(0, 5)

Important Notes

  • range is immutable.
  • It is mainly used in loops.
  • It supports indexing.
  • It is memory efficient.
  • It generates a sequence of integers.

Key Points

  • The range data type represents a sequence of numbers.
  • Python provides three forms of the range() function.
  • The step value can be positive or negative.
  • The range object supports indexing.
  • The range object is immutable.
  • Use list() to convert a range object into a list.

Quick Summary

Feature Description
Data Type range
Mutable No
Supports Indexing Yes
Memory Efficient Yes
Common Usage Loops
Represents Sequence of Numbers

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Data Type Summary