我如何在python中进行取幂?

Roh*_*bha 25 python math operators

def cube(number):
  return number^3
print cube(2)
Run Code Online (Sandbox Code Playgroud)

我会期待cube(2) = 8,但相反,我得到了cube(2) = 1

我究竟做错了什么?

Ste*_*all 68

^xor运算符.

** 取幂.

2**3 = 8

  • 还有内置的[pow](https://docs.python.org/3/library/functions.html#pow)和[math.pow](https://docs.python.org/3/library/ math.html#math.pow). (5认同)

Iro*_*ist 11

您也可以使用该math库.例如:

import math
x = math.pow(2,3) # x = 2 to the power of 3
Run Code Online (Sandbox Code Playgroud)