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

编程知识 行业动态 更新时间:2024-06-13 00:19:50

这里介绍一下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
"""

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

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

更多推荐

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

本文发布于:2023-03-28 23:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/c4759c4050d7f53ac911528ead1e3c6a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:次方   四种   方法   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!