0
点赞
收藏
分享

微信扫一扫

python中计算n次方运算的四种方法

m逆光生长 2022-07-14 阅读 65


这里介绍一下​​python​​中n次方运算的四种书写形式,代码如下:

# -*- coding:utf-8 -*-
"""
author: 15025
time: 10.08.2021 18:12:20
software: PyCharm

Description:
"""

import numpy as np


class PythonStudy:
@staticmethod
def powerAlgorithm():
# basic python method
x = 2
x1 = x ** 2
print(f"The value of x1 is {x1}")
x2 = pow(x, 2)
print(f"The value of x2 is {x2}")
x3 = x.__pow__(2)
print(f"The value of x3 is {x3}")
# numpy method
x4 = np.power(x, 2)
print(f"The value of x4 is {x4}")


if __name__ == '__main__':
main = PythonStudy()
main.powerAlgorithm()
"""
输出结果:
The value of x1 is 4
The value of x2 is 4
The value of x3 is 4
The value of x4 is 4
"""

可以看到,四种方法均成功输出了我们想要的平方运算。

码字不易,如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~


举报

相关推荐

0 条评论