How can I raise the numbers in list to a certain power?
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.
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.
Python pow() function returns the result of the first parameter raised to the power of the second parameter.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With