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 tostop - 1.range(start, stop)— fromstarttostop - 1.range(start, stop, step)— fromstarttostop - 1with a step ofstep.
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.