轻松学习Python 69个内置函数-pow()

it2023-02-16  78

# pow(base, exp[, mod]) # pow 是power 的缩写,exp 是 expoent的缩写,mod是module的缩写 # 返回 base 的 exp 次幂 print(f'{pow(2,3)=}') # pow(2,3)=8 print(f'{pow(10,2)=}') # pow(10,2)=100 # 两参数形式 pow(base, exp) 等价于乘方运算符: base**exp print(f'{2**3=}') # 2**3=8 print(f'{10**2=}') #10**2=100 # 如果 mod 存在,则返回 base 的 exp 次幂对 mod 取余(比 pow(base, exp) % mod 更高效) # 如果 mod 存在,会使用快速幂取模的算法来取模 import time start = time.time() print(f'{pow(1234,1234567,67) =}') #pow(1234,1234567,67) =2 end = time.time() # print(f'耗时:{end-start=}') #耗时:end-start=0.0 print(f'{pow(1234,1234567) %67=}') #pow(1234,1234567) %67=2 end = time.time() # print(f'耗时:{end-start=}') #耗时:end-start=3.2932801246643066 # 参数必须具有数值类型。 # print(f'{pow(2,"3")}') #TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'str' # 对于混用的操作数类型,则将应用双目算术运算符的类型强制转换规则。 print(f'{pow(2.0,3)=}') #pow(2.0,3)=8.0 # 对于 int 操作数,结果具有与操作数相同的类型(强制转换后),除非第二个参数为负值;在这种情况下,所有参数将被转换为浮点数并输出浮点数结果。 print(f'{pow(2,3)=}') #pow(2,3)=8 print(f'{pow(2,-3)=}') #pow(2,-3)=0.125 # 对于 int 操作数 base 和 exp,如果给出 mod,则 mod 必须为整数类型并且 mod 必须不为零。 print(f'{pow(2,3,0)=}') #ValueError: pow() 3rd argument cannot be 0

更多学习内容请查看:https://edu.csdn.net/course/play/31010/459393

最新回复(0)