Nearby lessons
26 of 108Python - 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
- Represents a sequence of numbers.
- Immutable.
- Commonly used in loops.
- Memory efficient.
- 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
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
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
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
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
10
Invalid Index Example
main.py
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
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
[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
Hello Hello Hello Hello Hello
Example - Print Even Numbers
main.py
0 2 4 6 8 10
Example - Print Odd Numbers
main.py
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
10
Advantages of range
- Memory efficient.
- Faster iteration.
- Useful in loops.
- 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
[0, 1, 2, 3, 4]
Example - range
main.py
range(0, 5)
Important Notes
rangeis immutable.- It is mainly used in loops.
- It supports indexing.
- It is memory efficient.
- It generates a sequence of integers.
Key Points
- The
rangedata type represents a sequence of numbers. - Python provides three forms of the
range()function. - The step value can be positive or negative.
- The
rangeobject supports indexing. - The
rangeobject 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 |