Why is that duplicating instead of multiplication?


#1
def m(v):
    return 2*v*2

my=[0,1,2,10,100]
print (m(my))

#2

Hi I think taht it's because v which is the parameter is equale to the argument [0,1,2,10,100] so inside the function it's like that

return 2*[0,1,2,10,100]*2

SO it right that it give you .. Because it make the multiplication of the [0,1,2,10,100]

[0, 1, 2, 10, 100, 0, 1, 2, 10, 100, 0, 1, 2, 10, 100, 0, 1, 2, 10, 100]

so if you want it do the multiplication try it like that

def m(v):
    return 2*v*2

print (m(8))

#Output 
32

#3

Thanks,
But you suppose that I want to use multiplication for a list like above. In this why how can I multiply list by 2 or 4?


#4

you can do it with a for loop like that

my=[0,1,2,10,100]
for num in my:
    print(num * 2)
#Output 
0
2
4
20
200

#5

Thanks,
Is there any way other than writing loop?


#6

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