1、**
"""
# 平方的多种方法
"""
# 1、**
# num=5
num = 5
res=num**2
print(res) # 25 # 25.0
2、pow()
# 2、pow(base,exp,mod=1) 等同于base**exp%mod
# 即base的exp次幂,除以mod后的余数
res=pow(num,2)
print(f"{num}的平方是:",res)
# 5.0的平方是: 25.0
# 5的平方是: 25
# mod的使用
# res3=pow(num,3,5)
print(f"pow({num},3,5)={pow(num,3,5)}") # pow(5,3,5)=0
print(f"pow({num},3,3)={pow(num,3,3)}") # pow(5,3,3)=2
3、math模块的pow
# 3、math.pow(x,y) x的y次方 Return x**y (x to the power of y).
import math
res4=math.pow(num,2)
print(num,res4) # 5 25.0
4、参考
1、Python平方的几种写法|极客教程