我们在用函数调用的时候一般是要考虑函数的返回值,其次就是参数的问题。相信经过之前几篇参数文章的讲解,大部分小伙伴已经对参数不陌生了。那么今天我们就来讲讲常见返回值的问题。这里以pow函数的调用为例,在学习使用方法的同时,我们会试着找寻内置调用返回值得结果以及进行结果分析。
pow() 方法返回 xy(x 的 y 次方) 的值。
math 模块 pow() 方法的语法:import math
math.pow(x, y)
内置的 pow() 方法:pow(x, y[, z])
函数是计算 x 的 y 次方,如果 z 在存在,则再对结果进行取模,其结果等效于 pow(x,y) %z。
注意:pow() 通过内置的方法直接调用,内置方法会把参数作为整型,而 math 模块则会把参数转换为 float。
举例:# pow(base, exp[, mod])
# pow power exp exponent mod module
# 返回 base 的 exp 次幂;如果 mod 存在,则返回 base 的 exp 次幂对 mod 取余(比 pow(base, exp) % mod 更高效)。 两参数形式 pow(base, exp) 等价于乘方运算符: base**exp。
# 返回 base 的 exp 次幂;
print(f'{ pow(2, 3) = }')
print(f'{ pow(10, 2) = }')
# 两参数形式 pow(base, exp) 等价于乘方运算符: base**exp。
print(f'