def fun1(a):
for i in range(len(a)):
a[i] = a[i] * a[i]
return a
views:
180answers:
6It takes an array as parameter and returns the same array with each member squared.
EDIT:
Since you modified your question from 'What does this function do' to 'What is some code to execute this function', here is an example:
def fun1(a):
for i in range(len(a)):
a[i] = a[i] * a[i]
return a
test1 = [1,2,3,4,5]
print 'Original list', test1
test2 = fun1(test1)
print 'Result', test2
print 'Original list', test1
The output will be:
Original list [1, 2, 3, 4, 5]
Result [1, 4, 9, 16, 25]
Original list [1, 4, 9, 16, 25]
Because the function modifies the list in place, test1 is also modified.
it multiplies each element of the array "a" with itself and stores the results back in the array.
a is passed as a list , I assume.
It squares each element of the list and returns the list.
It will go through your List and multiply each value by itself.
Example
a = [ 1, 2, 3, 4, 5, 6 ]
After that function a would look like this:
a = [ 1, 4, 9, 16, 25, 36 ]
It squares every element in the input array and returns the squared array.
So with a = [1,2,3,4,5]
result is: [1,4,9,16,25]
It's a trivial function that could be replaced with the one-liner:
a = [x*x for x in a]