# Range(start, stop, step)

#1

Aloha,

in lesson ‘Passing a range into a function’ there is 3 type of range() function -
range(stop)
range(start, stop)
range(start, stop, step)

what this ‘step’ doing? I can not understand what ‘step’ does in function. we have an example: “range (1, 6, 3) # => [1, 4]” why only returns 1 and 4? can anybody explain it in more detail?

I can not understand this sentence: "In all cases, the range () function returns a list of numbers from the start up to (but not including) stop. Each item increases by step. If omitted, start defaults to 0 and step defaults to 1. "

thanks

#2

Since the start value is 1, it begins with 1. Then, since the step is 3, each value is 3 more than the previous value. That gives us 4 for the second value. If there were a value after that, it would be 7, except that the sequence is limited by the stop value of 6.

now my question is:
if we add function like this ===> range(1, 10, 3) it return 1, 4, 7 ? is that correct?

#3

Almost. The sequence is correct but it will be contained in a data structure that supports a sequence… an iterable.

In Python 2, `range()` will return a list (an iterble object)…

``````>>> range(1, 10, 3)
[1, 4, 7]
>>>
``````

In Python 3, `range()` will return an iterator which we can invoke with the `list()` function.

``````>>> range(1, 10, 3)
range(1, 10, 3)
>>> list(range(1, 10, 3))
[1, 4, 7]
>>>
``````
``````range(start, stop, step)
``````

`start` is included in the range
`stop` is not included in the range
`step` must be an integer

``````>>> n=range(1.0, 2.0, 0.1)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
n = range(1.0, 2.0, 0.1)
TypeError: 'float' object cannot be interpreted as an integer
>>>
``````

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.