Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raising elements of a list to a power [closed]

How can I raise the numbers in list to a certain power?

like image 321
Amelia155 Avatar asked May 19 '15 10:05

Amelia155


People also ask

How do you raise something to a power in Python?

The ** operator in Python is used to raise the number on the left to the power of the exponent of the right. That is, in the expression 5 ** 3 , 5 is being raised to the 3rd power.

How do you write 10 to the power in Python?

1. The Double Asterisk (**) Operator. You can use the double-asterisk operator to raise a number to a power in Python. This is a clear and efficient way to compute powers in Python.

How do you write 2 power N in Python?

Python pow() function returns the result of the first parameter raised to the power of the second parameter.


1 Answers

Use list comprehension:

def power(my_list):
    return [ x**3 for x in my_list ]

https://docs.python.org/3.4/tutorial/datastructures.html#list-comprehensions

like image 162
Jorick Spitzen Avatar answered Sep 27 '22 19:09

Jorick Spitzen