Course Content
Introduction to Python
Introduction to Python
Range Function in Python
To iterate over indices instead of elements, the range()
function is essential. It generates a sequence of numbers and returns a range
object. This function accepts one, two, or three numeric arguments. With a single argument n
, range(n)
produces integers from 0
up to, but not including, n
.
For example, range(5)
generates the numbers 0
to 4
.
# Range with one argument for i in range(5): print(i, end = ' ')
When range()
is given two arguments, n
and m
, it generates integers starting from n
up to, but not including, m
.
For example, range(5, 10)
produces the numbers 5 through 9.
# Range with two arguments for i in range(5, 10): print(i, end = ' ')
When range()
receives three arguments n
, m
, and s
it generates numbers starting from n
, up to but not including m
, incrementing by s
each time.
For example, range(10, 30, 5)
produces the sequence 10, 15, 20, 25.
# Range with three arguments for i in range(10, 30, 5): print(i, end = ' ')
Thanks for your feedback!