Sobes.tech
Junior — Middle

Can you explain what the term range means in Python and how it is used?

sobes.tech AI

Answer from AI

In Python, range is a built-in function that generates a sequence of numbers. It is often used to organize loops with a specific numerical range.

Main usage options:

  • range(stop) — creates a sequence from 0 to stop - 1.
  • range(start, stop) — from start to stop - 1.
  • range(start, stop, step) — from start to stop - 1 with a step of step.

Example:

for i in range(1, 5):
    print(i)

It will output numbers from 1 to 4 inclusive.

range returns an iterator object, which saves memory because it does not store the entire sequence at once.