本文主要是介绍python中计算n次方运算的四种方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这里介绍一下python
中n次方运算的四种书写形式,代码如下:
# -*- coding:utf-8 -*-
"""
author: 15025
time: 10.08.2021 18:12:20
software: PyCharmDescription:
"""import numpy as npclass PythonStudy:@staticmethoddef powerAlgorithm():# basic python methodx = 2x1 = x ** 2print(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 methodx4 = 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
"""
可以看到,四种方法均成功输出了我们想要的平方运算。
码字不易,如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~
这篇关于python中计算n次方运算的四种方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!