+91 81900 98289

Python Range

This Python Range lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

range does not build a full list immediately. It produces numbers as you ask for them. list(range(...)) shows them all.

The third argument is the step. A negative step counts down.

Example 1: Start here

range(1, 6) stops at 5.

print(list(range(1, 6))) # print writes this; the quotes are not shown print(list(range(5, 0, -1)))

Example 2: Stop is excluded

range(1, 5) is 1, 2, 3, 4.

print(list(range(1, 5))) # print writes this; the quotes are not shown

Example 3: A step

The third number is the step, so this prints the even numbers.

print(list(range(0, 10, 2))) # print writes this; the quotes are not shown

Example 4: Count backward

A negative step counts down. The stop is still excluded, so 0 is not included.

print(list(range(3, 0, -1))) # print writes this; the quotes are not shown
range(1, 6) stops at 5.
Call Now 81900 98289