Python提供了计算数字幂的功能,因此可以简化计算数字幂的任务。它在日常编程中具有许多应用程序。

天真的计算能力的方法:

# Python code to demonstrate naive method

# to compute power

n = 1

for i in range(1,5):

n=3*n

print ("The value of 3**4 is:",end="")

print (n)

输出:

The value of 3**4 is:81

使用pow()

1. float pow(x,y):此函数计算x ** y。此函数首先将其参数转换为float,然后计算幂。

Declaration:

float pow(x,y)

参数:

x: Number whose power has to be calculated.

y: Value raised to compute power.

返回值:

Returns the value x**y in float.

# Python code to demonstrate pow()

# version 1

print ("The value of 3**4 is:",end="")

# Returns 81

print (pow(3,4))

输出:

The value of 3**4 is:81.0

2. float pow(x,y,mod):此函数计算(x ** y)%mod。此函数首先将其参数转换为float,然后计算幂。

Declaration:

float pow(x,y,mod)

参数:

x: Number whose power has to be calculated.

y: Value raised to compute power.

mod: Value with which modulus has to be computed.

返回值:

Returns the value (x**y) % mod in float.

# Python code to demonstrate pow()

# version 2

print ("The value of (3**4) % 10 is:",end="")

# Returns 81%10

# Returns 1

print (pow(3,4,10))

输出:

The value of (3**4) % 10 is:1

pow()中的实现案例:

# Python code to discuss negative

# and non-negative cases

# positive x, positive y (x**y)

print("Positive x and positive y:",end="")

print(pow(4, 3))

print("Negative x and positive y:",end="")

# negative x, positive y (-x**y)

print(pow(-4, 3))

print("Positive x and negative y:",end="")

# positive x, negative y (x**-y)

print(pow(4, -3))

print("Negative x and negative y:",end="")

# negative x, negative y (-x**-y)

print(pow(-4, -3))

输出:

Positive x and positive y:64

Negative x and positive y:-64

Positive x and negative y:0.015625

Negative x and negative y:-0.015625

python中pow函数用法_Python pow()用法及代码示例相关推荐

  1. python中type(12.34)_Python typing.TYPE_CHECKING属性代码示例

    本文整理汇总了Python中typing.TYPE_CHECKING属性的典型用法代码示例.如果您正苦于以下问题:Python typing.TYPE_CHECKING属性的具体用法?Python t ...

  2. python中stringvar的用法_Python tkinter.StringVar方法代码示例

    本文整理汇总了Python中tkinter.StringVar方法的典型用法代码示例.如果您正苦于以下问题:Python tkinter.StringVar方法的具体用法?Python tkinter ...

  3. python中bind的用法_Python socket.bind方法代码示例

    本文整理汇总了Python中socket.bind方法的典型用法代码示例.如果您正苦于以下问题:Python socket.bind方法的具体用法?Python socket.bind怎么用?Pyth ...

  4. python中sort函数源代码_Python中sort和sorted函数代码解析

    Python中sort和sorted函数代码解析 本文研究的主要是Python中sort和sorted函数的相关内容,具体如下. 一.sort函数 sort函数是序列的内部函数 函数原型: L.sor ...

  5. python end用法_Python turtle.end_fill方法代码示例

    本文整理汇总了Python中turtle.end_fill方法的典型用法代码示例.如果您正苦于以下问题:Python turtle.end_fill方法的具体用法?Python turtle.end_ ...

  6. python中format函数作用_python中format函数什么意思

    python中format函数什么意思? Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前的 % ...

  7. python中return函数换行_Python中return的用法

    python语言中,在调用函数的时候,为了使得函数返回我们想要的值,就需要python中的return来解决此问题.对于return的使用有以下需要注意的事项. (1)return语句是python语 ...

  8. python中formatter的用法_Python pyplot.FuncFormatter方法代码示例

    本文整理汇总了Python中matplotlib.pyplot.FuncFormatter方法的典型用法代码示例.如果您正苦于以下问题:Python pyplot.FuncFormatter方法的具体 ...

  9. python中geometry用法_Python geometry.Point方法代码示例

    本文整理汇总了Python中shapely.geometry.Point方法的典型用法代码示例.如果您正苦于以下问题:Python geometry.Point方法的具体用法?Python geome ...

  10. python中font的用法_Python font.nametofont方法代码示例

    本文整理汇总了Python中tkinter.font.nametofont方法的典型用法代码示例.如果您正苦于以下问题:Python font.nametofont方法的具体用法?Python fon ...

最新文章

  1. 如何开发属于自己的第一个Java程序
  2. java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
  3. 让数字保持在整数范围内
  4. CSS3学习手记(10) 过渡
  5. 《剑指offer》面试题46、47、49
  6. oracle 负载均衡连接方式常用SQL语句备忘录
  7. mysql generator 命令_Mybatis使用命令生成逆向工程的方法
  8. vue css下载字体并引入使用
  9. 惯性导航解算程序C++实现
  10. (原博客转移)诺基亚手机成板砖无法开机后,强刷修复手机系统的方法!本人亲测
  11. matlab画全球参数分布图 绘制世界地图
  12. numpy 折线图绘制(参考)
  13. input框中的文字加下划线
  14. 使用Python发送邮件(QQ邮箱为例)
  15. 用友t3服务器地址在哪里修改,畅捷通T+pos端后续想更换服务器地址链接,怎么操作?...
  16. 福州大学java期末试卷,Java Web认证题.doc
  17. 深耕ElasticSearch - 认识搜索引擎的索引
  18. 【转】最全网上纳税申报流程
  19. ip addr命令解析
  20. 思维导图 基础篇(05)思维方法-高度思维

热门文章

  1. gprs模块与服务器通信协议,gprs模块与服务器通信原理分析
  2. 牛年新生儿取名:用诗经给男宝宝起名字
  3. oracle11g64跟32,plsql32 位连接oracle11g64位方法
  4. Java 枚举和泛型
  5. android二维码图片生成模糊的问题
  6. ICommand和ITool命令操作地图
  7. ffmpeg 为取经而来_孙悟空很早之前就死了,取经的其实是六耳猕猴,这说法是真的吗?...
  8. PEGA 十合一吉他控制器连接电脑玩《吉他英雄3》
  9. 关于for丶foreach丶iterator 迭代器
  10. 《动手学深度学习》(PyTorch版)代码注释 - 52 【World2Vec_Learning】