# Putting a range into a function

#1

``````# I do not get how to pass a range into a function or how the start, stop, step thing works.

def my_function(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
return x

# This is my code and I was just trying a bunch of numbers to attempt to learn by trial and error but I put in range(3) and for some reason that worked.

[0, 2, 4]
None

# This is what my code returned and it worked although the code was supposed to return [0, 1, 2]. Some help and explanation would be much appreciated.``````

#2

When you type `range(3)` you are defining a list of integers between 0 and 2. i.e. `range(3)=[0,1,2]`
With the `range(start, stop, step)` you can define where you want your list to start, end and the size of the step between terms. If you don't include a value for the step, then the list assumes a default of 1. Here are some examples below:

``range(0,7,2)=[0,2,4,6]``

``range(0,13,3)=[0,3,6,9,12]``

You can also have non-integer values for the step:

``range(0,4,0.5)=[0,0.5,1,1.5,2,2.5,3,3.5]``

What your function is doing is the following:

1. Function takes an list `x` as an argument.
2. Function loops over the length of the list and resets `x[i]` which is the `i`th term in `x` to a value of `x[i]*2`
3. The function returns the new list in which now each term is double that of the one you initially passed.

I hope this helps you have an idea of the range you are supposed to pass to the function in order to return a list of `[0,1,2]`?

#3

Thank you very much it makes sense now.

#4

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