Python range() function

overview of range()

The range() function returns a sequence of numbers between the given start and stop values. By default the range() function starts at 0.

use of range()

The range() function can be used quite flexibly depending on the given arguments. # use of range() function # range from 0 - 5 print(list(range(6))) # range from 1 - 6 print(list(range(1, 7))) # range from 2 - 10 but only even numbers print(list(range(2, 11, 2))) The range() function is used to create the following sequences using the start, stop and step arguments of the function. [0, 1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 6] [2, 4, 6, 8, 10]

syntax of range()

The syntax of the range() function depends on the number of arguments passed. If one argument is passed, it is used as stop value and the syntax is: range(stop) If two or three arguments are passed, the syntax of the range() function is: range(start, stop[, step])

arguments of range()

The range() function accepts between 1 and 3 arguments.
argument required description
start optional the integer value from which the sequence begins
stop required the integer value at which the sequence ends -1
step optional the increment between each integer in the sequence

return values of range()

The range() function returns an immutable sequence of numbers for the given arguments. The returned object is a range object, which can be turned in a list using the list() function.



Click to jump to section