What is the purpose of range() function? Give one example.
The range() function is a built-in function in Python used to create a sequence of integers. It generates numbers from a given start value up to a stop value (excluding the stop value), with a difference specified by the step value.
The syntax is range([start], stop[, step]). The start and step parameters are optional. If start is not specified, the sequence begins from 0 by default. If step is not specified, the value increases by 1 in each iteration.
For example, range(10) generates a sequence from 0 to 9. When used as list(range(10)), it produces: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. The range() function is often used in for loops for generating a sequence of numbers.
Explanation
The answer explains the purpose of range() as stated in the textbook - it creates a sequence of integers from start to stop (excluding stop) with a given step difference. The example shows the simplest usage where only the stop value is specified, generating numbers 0 to 9. The textbook also mentions that range() is commonly used in for loops.