Passing a range into a function


#1

Hello friends,

I could not understand the concept of range function and i feel confusing with index,whether it starts with 0 or starts with 1. So. couldnot move further. I request friends to help me and also clear the doubt of how range function works. The task given in the exercise is to On line 6, replace the ____ with a range() that returns a list containing [0, 1, 2].
I am not sure whether i missed out to give the list x in the program

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

print my_function( )) # Add your range between the parentheses!

#2

there is one simple way to find out where the range function starts, run:

print range(3)

this will output:

[0, 1, 2]

so counting starts at zero, now i sort of gave a away what you need to do with your function call. range is a shorthand for a for loop, range(3) converted to a for loop would be:

for (int i = 0; i < 3; i++)

which will also output 0,1,2

You could read about range in the official python documentation (link)


#3

Thanks. So range is used to store values same as its index basically I mean if we don't manipulate the values inside the function. It will contain the values same as its index. Please correct me if i am wrong.


#4

You have a function which you pass a argument x:

def my_function(x):

and then when you call your function:

print my_function(range(3))

now range is a list ([0,1,2]) (which you can see if you do: print range(3)), which is why you have a for loop in your in your function, does that make sense?


#5

Thanks a lot. I have completed the task. I have understood that range is used to generate lists which is passed to a function. Inside function for loop is used to modify values.

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

#6

uhm... no? It should be:

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

range is a list, you can parse it into the function directly. Then your for loop will go over the list, and multiply everything by two


#7

yeah, I agree. But i have calculated length using the function len(x), into which list object is passed. Obviously, it evaluates to 3. It is also correct right


#8

But the beauty of this code is that you can simple pass a bigger range in the function if you want that:

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

len(x) will make sure all numbers in the list will get the * 2 treatment.


#9

yeah absolutely,thanks. Then there is no need to write the stament x=[0,1,2]. Becasue range itself generate the list.Please see my rewritten code as below and let me know your suggestions

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

#10

That is the right code. range generates the list indeed, so no need to define that


#11

@stetim94. Thanks a lot for your explaination


#13