Nearby lessons
22 of 159Python - Range Data Type
- Define the range data type and what it represents
- Use the three forms of the range() function
- Create ranges with positive and negative step values
- Access range elements using indexing
- Convert a range object to a list with list()
- Explain why range is immutable and memory efficient
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)
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)
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
Explanation
Here, 2 is the increment value.
range(10, 20, 2) starts from 10, increases by 2, and stops before 20.
Example - Negative Step
Accessing range Elements Using Index
A range object supports indexing.
We can access elements using positive indexes.
Example - Indexing
Invalid Index Example
range is Immutable
Elements of a range object cannot be modified after creation.
Example - Invalid Modification
Converting range to List
We can convert a range object into a list using the list() function.
Example - list() Function
Using range in for Loop
The range() function is mainly used with for loops.
Example - Print Hello
Example - Print Even Numbers
Example - Print Odd Numbers
Length of range Object
Use the len() function to find the number of elements in a range object.
Example - len() Function
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
Example - range
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 |
- The range data type represents an immutable sequence of numbers
- range(stop) generates numbers from 0 to stop - 1
- The step value in range() can be positive or negative
- Use list() to convert a range object into a list
- range is memory efficient because it generates values dynamically
- The range data type is mainly used with for loops