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)

Loading editor…

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)

Loading editor…

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

Loading editor…

Explanation

Here, 2 is the increment value.

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

Example - Negative Step

Loading editor…

Accessing range Elements Using Index

A range object supports indexing.

We can access elements using positive indexes.

Example - Indexing

Loading editor…

Invalid Index Example

Loading editor…

range is Immutable

Elements of a range object cannot be modified after creation.

Example - Invalid Modification

Loading editor…

Converting range to List

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

Example - list() Function

Loading editor…

Using range in for Loop

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

Example - Print Hello

Loading editor…

Example - Print Even Numbers

Loading editor…

Example - Print Odd Numbers

Loading editor…

Length of range Object

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

Example - len() Function

Loading editor…

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

Loading editor…

Example - range

Loading editor…

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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.