博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python pow_Python pow()
阅读量:2536 次
发布时间:2019-05-11

本文共 2286 字,大约阅读时间需要 7 分钟。

python pow

Python pow() function usually takes two arguments and returns their power.

Python pow()函数通常采用两个参数并返回其幂。

Python pow() (Python pow())

Python pow() function syntax is:

Python pow()函数语法为:

pow(x, y[, z])
  • If only two arguments are provided, then x to the power of y is returned. In this case, they can be integers, floats, and complex numbers. The two-argument form pow(x, y) is equivalent to using the power operator: x**y.

    如果仅提供两个参数,则返回x乘以y的幂。 在这种情况下,它们可以是整数,浮点数和复数。 二元形式pow(x,y)等效于使用幂运算符:x ** y。
  • If three arguments are provided, then x to the power y, modulo z is returned. It’s computed more efficiently than using pow(x, y) % z.

    如果提供了三个参数,则将x乘以y,以z为模。 它的计算比使用pow(x,y)%z更有效。
  • If z is present, x and y must be of integer types, and y must be non-negative.

    如果存在z,则x和y必须为整数类型,并且y必须为非负数。

Let’s look into some pow() function examples.

让我们看一些pow()函数示例。

带有整数的Python pow() (Python pow() with integers)

print(pow(10, 2))print(pow(-10, 3))print(pow(10, 2, 3))

Output:

输出:

100-10001

带有浮点数的Python pow() (Python pow() with floats)

print(pow(100, -1))print(pow(100.0, 2))

Output:

输出:

0.0110000.0

pow()具有不同的格式整数 (pow() with different format integers)

print(pow(0b11, 2))  # 0b11 = 3print(pow(0b11, 2, 2))print(pow(0o11, 2))  # 0o11 = 9print(pow(0o11, 2, 2))print(pow(0xF, 2))  # 0xF = 15print(pow(0xF, 2, 2))

Output:

输出:

918112251

pow()与复数 (pow() with complex numbers)

print(pow(2 + 3j, 2))

Output: (-5+12j)

输出: (-5+12j)

pow()异常情况 (pow() exception scenarios)

  1. print(pow(2 + 3j, 2, 3))

    Error: ValueError: complex modulo

    错误: ValueError: complex modulo

  2. print(pow(100.0, 2, 3))

    Error: TypeError: pow() 3rd argument not allowed unless all arguments are integers

    错误: TypeError: pow() 3rd argument not allowed unless all arguments are integers

  3. print(pow(100, -1, 2))

    Error: ValueError: pow() 2nd argument cannot be negative when 3rd argument specified

    错误: ValueError: pow() 2nd argument cannot be negative when 3rd argument specified

pow()与math.pow() (pow() vs math.pow())

Python also has a pow() function but the built-in function is more powerful because we can perform modulo operation too after power. Also, we don’t need to import math module for a single functionality.

Python 还具有pow()函数,但是内置函数更强大,因为我们在通电后也可以执行模运算。 另外,我们不需要为单个功能导入数学模块。

. 检出完整的python脚本和更多Python示例。

Reference:

参考:

翻译自:

python pow

转载地址:http://cxmzd.baihongyu.com/

你可能感兴趣的文章
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>