• Python

Command Palette

Search for a command to run...

Native Functions
  • Python Abs Function
  • Python Any All Functions
  • Python Zip Function
  • Python Sum Function
  • Python Filter Function
  • Python Max Min Functions
  • Python Map Function
  • Python Round Function
  • Python Sorted Function
Data Types
  • Python Integers
  • Python Floats
  • Python Complex Numbers
  • Python List Type
  • Python Tuple Type
  • Python String Type
  • Python Range Type
Collections.abc Module
  • Python Containers
  • Python Hashable
  • Python Iterable
  • Python Iterators
  • Python Sequence
  • Python Mutable Sequence

Create an Account

FREE

Join our community to access more courses.

Create Account

On this page

What Is a Range?Creating RangesAccessing Range ValuesCommon Range OperationsIterating Over RangesRange AttributesComparing RangesSummary
      • Blog

      Python Range Type

      What Is a Range?

      A range object represents an immutable sequence of numbers, commonly used for looping a specific number of times in for loops. Ranges are memory-efficient because they only store the start, stop, and step values, not the entire sequence.

      Creating Ranges

      You can create a range using the range() constructor:

      r1 = range(5)            # 0, 1, 2, 3, 4
      r2 = range(1, 6)         # 1, 2, 3, 4, 5
      r3 = range(0, 10, 2)     # 0, 2, 4, 6, 8
      r4 = range(10, 0, -1)    # 10, 9, 8, ..., 1
      
      • The arguments are start, stop, and optional step (default is 1).

      Accessing Range Values

      You can convert a range to a list or tuple, or access items by index:

      r = range(3, 8)
      print(list(r))      # [3, 4, 5, 6, 7]
      print(r[0])         # 3
      print(r[-1])        # 7
      print(r[1:4])       # range(4, 7)
      print(list(r[1:4])) # [4, 5, 6]
      

      Common Range Operations

      Ranges support many sequence operations:

      r = range(5)
      print(len(r))         # 5
      print(3 in r)         # True
      print(r.index(3))     # 3
      print(r.count(2))     # 1
      

      Iterating Over Ranges

      Ranges are most often used in for loops:

      for i in range(3):
          print(i)
      

      Range Attributes

      Range objects have three attributes:

      r = range(1, 10, 2)
      print(r.start)  # 1
      print(r.stop)   # 10
      print(r.step)   # 2
      

      Comparing Ranges

      Two ranges are equal if they represent the same sequence of values:

      print(range(0, 3, 2) == range(0, 4, 2))  # True
      

      Summary

      • Ranges represent sequences of numbers, defined by start, stop, and step.
      • Memory-efficient, even for large ranges.
      • Support indexing, slicing, and sequence operations.
      • Commonly used for looping a specific number of times.

      Continue Learning

      Python Variables

      Popular

      Getting Started: Understanding Variables in Python In programming, we often need to store informatio

      Python Arguments

      For You

      Providing Input to Functions In the previous article, we learned that functions are reusable blocks

      Python Functions

      For You

      Organizing Code: Understanding Functions In programming, we often need to perform the same set of ac

      Personalized Recommendations

      Log in to get more relevant recommendations based on your reading history.