Code explanation: We used all three parameters: start
, stop
, and step
. The loop starts at 1, increments by 2 each time (due to the step
value), and stops before reaching 10.
What happens when you reverse the step value? Try experimenting with these changes and observe the change in the output.
This will help deepen your understanding of how range()
handles different parameters and how it can be used to create loops that count downwards.
xrange()
function
The xrange()
function is available only in Python 2. It works like range()
, but it returns a generator object instead of a list. This means it creates numbers one at a time when needed, making it very efficient for large ranges.
Syntax of xrange()
function
The syntax of xrange()
is the same as range()
: