Python range()

Python range type represents an immutable sequence of numbers. The most common use of a range is to use it for iterating over a series of items (list, set, tuple, dictionary or string).

The range object is more memory efficient than list or tuple. A range stores only the start, stop and step values in memory. The actual sequence values or sub-ranges are calculated in runtime, as needed.

1. Python range() Quick Reference

The given below code demonstrate the uses of range() function. It shows the valid and invalid uses of the method, accessing the elements in a range sequence, looping over them, and other operations such as slicing.

###### range is a type in Python

# <class 'range'>
type_info = type(range(10)) 

###### Loop through range

# 0 1 2 3 4 5 6 7 8 9
x = range(10)
for n in x:
  print(n, end=" ")     

###### Valid Usages

# 0 1 2 3 4 5 6 7 8 9
range(10)        

# 2 3 4 5 6 7 8 9
range(2, 10)

# 2 4 6 8
range(2, 10, 2)

# 10 8 6 4
range(10, 2, -2)

####### Invalid Usages

# Floats are not allowed
# range(3.3)

# Characters are not allowed
# range('a', 'd')

####### Access items in range

x = range(10)

# First item in range
print( x[0] )   # 0

# Last item in range
print( x[-1] )  # 9

# 4th item in range
print( x[3] )   # 3

# Check if a number is part of the sequence
print (12 in x)     # False
print (8 in x)      # True

# Supports index()
print( x.index(3) )   # 3

###### Supports slicing

print( x[5] )       # 5
print( x[2:] )      # range(2, 10)
print( x[:5] )      # range(0, 5)
print( x[2:5] )     # range(2, 5)
print( x[-1] )      # 9

2. Python range() Syntax

The range() method supports two syntax variations.

  • range(stop) : Generates a sequence of integers from 0 to stop-1, increments by 1.
  • range(start, stop[, step]) : Generates a sequence of integers from start to stop-1, increments by step.

Please note that:

  • All parameters must be integers (positive or negative).
  • start is inclusive number. It means that start will be part of the sequence and will be the first number in the sequence.
  • stop is exclusive number. It means that stop will NOT be part of the sequence. The sequence will terminate exactly before the stop number.
  • Sequence obtained from range() follows zero-base indexes. It means that in a sequence of n numbers, indexes will start from 0 and end at n-1.
  • step is optional parameter. If step is zero, ValueError is raised.

3. Python range() Examples

3.1. Python program to create range between two numbers

In the given python program, we are creating a range of numbers from 2 to 10. we are then using for-loop to print the numbers in the range.

r = range(2, 10)
for n in r:
    print(n, end=" ")
print()

Program output.

2 3 4 5 6 7 8 9

3.2. Python program to reverse a range

In the given python program, we are creating a reverse range from a given range of numbers.

r = range(2, 10)
for n in r:
    print(n, end=" ")
print()

rr = reversed(r)
for n in rr:
    print(n, end=" ")
print()

Program output.

2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2

3.3. Comparing two ranges for equality

Two Python ranges are equal if they will generate the same sequence of numbers.

In the given python program, we are comparing two ranges for the numbers, they will generate in the sequence.

r1 = range(1, 2)        # [1]
r2 = range(1, 5, 10)    # [1]

print(r1 == r2)

Program output.

True

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.