Nearby lessons

22 of 159

Python - Range Data Type

📌 What You Will Learn
  • 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

  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)

🐍Code Cell
1r = range(10)
2 
3for i in r:
4 print(i)
Output
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)

🐍Code Cell
1r = range(10, 20)
2 
3for i in r:
4 print(i)
Output
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

🐍Code Cell
1r = range(10, 20, 2)
2 
3for i in r:
4 print(i)
Output
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

🐍Code Cell
1r = range(10, 0, -1)
2 
3for i in r:
4 print(i)
Output
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

🐍Code Cell
1r = range(10, 20)
2 
3print(r[0])
Output
10

Invalid Index Example

🐍Code Cell
1r = range(10, 20)
2 
3print(r[15])
Output
IndexError: range object index out of range

range is Immutable

Elements of a range object cannot be modified after creation.

Example - Invalid Modification

🐍Code Cell
1r = range(10)
2 
3r[0] = 100
Output
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

🐍Code Cell
1l = list(range(10))
2 
3print(l)
Output
[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

🐍Code Cell
1for i in range(5):
2 print("Hello")
Output
Hello
Hello
Hello
Hello
Hello

Example - Print Even Numbers

🐍Code Cell
1for i in range(0, 11, 2):
2 print(i)
Output
0
2
4
6
8
10

Example - Print Odd Numbers

🐍Code Cell
1for i in range(1, 11, 2):
2 print(i)
Output
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

🐍Code Cell
1r = range(10)
2 
3print(len(r))
Output
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

🐍Code Cell
1l = [0, 1, 2, 3, 4]
2 
3print(l)
Output
[0, 1, 2, 3, 4]

Example - range

🐍Code Cell
1r = range(5)
2 
3print(r)
Output
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
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8