Skip to content
Exercises · Q2

Q.What is the purpose of range() function? Give one example.

Yanam BieapTextbookSubjective· 2mImportance★★★★★est
64% · 25/39 Questions
✓ Free question

range() generates an immutable sequence of integers from start (default 0) up to but not including stop, in the given step (default 1) — the standard way to run a for loop a controlled number of times.

Purpose. range() creates a sequence of whole numbers on demand. Because a for loop in Python iterates over a sequence, range() is the natural tool whenever we want a loop to run a specific number of times or to count over index positions.

Its three forms:

FormMeaningExampleSequence produced
range(stop)0 up to stop-1range(5)0, 1, 2, 3, 4
range(start, stop)start up to stop-1range(2, 6)2, 3, 4, 5
range(start, stop, step)with a step (may be negative)range(10, 0, -2)10, 8, 6, 4, 2

Two things to remember: the stop value is never included, and all three arguments must be integers.

Example:

for i in range(1, 6):
    print(i, end=" ")
1 2 3 4 5

The loop body runs exactly 5 times with i taking the values 1 through 5; 6 (the stop) is excluded.

You can also materialise the sequence to inspect it at the interpreter:

>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
Watch out

Off-by-one errors come from forgetting that stop is excluded: to loop over 1…10 you need range(1, 11), not range(1, 10).

✓Final answer

The purpose of range() is to generate an integer sequence (start to stop-1 in steps of step) for controlled repetition in for loops — e.g. for i in range(1, 6): print(i) prints 1 2 3 4 5.

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.