Fran Ahedo Guerrero Fran Ahedo Guerrero - 8 days ago 4
Python Question

How to square a component-to-component vector? Python

I want to make the square of a vector, for example this

n = (1,2,3,4,5,6,7,8,9,10)
and that as a solution
n = (1,4,9,16, 25.36,49,64,81,100)

Answer

You need to use list comprehension:

n = (1,2,3,4,5,6,7,8,9,10)

new_n = tuple(pow(i, 2) for i in n)

Output:

(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)