python语言的幂运算符_5.4 幂运算符 The power operator 5.5 一元算术运算符 Unary arithmetic operations - Python 语言参考手册 - ...

5.4 幂运算符 The power operator

The power operator binds more tightly than unary operators on

its left; it binds less tightly than unary operators on its right.

The syntax is:

幂运算符比在操作数左边的一元运算符有更高的优先级; 但比右面的一元运算符要低.句法为:

Thus, in an unparenthesized sequence of power and unary

operators, the operators are evaluated from right to left (this

does not constrain the evaluation order for the operands).

因此, 在一串没有括号的由幂运算符和一元运算符组成的序列, 会从左到右面求值(没有强制改变求值顺序).

The power operator has the same semantics as the built-in

pow() function, when called with two

arguments: it yields its left argument raised to the power of its

right argument. The numeric arguments are first converted to a

common type. The result type is that of the arguments after

coercion.

当以两个参数调用pow() 时, 幂运算符与内建函数 pow()有相同的语义:

生成左边参数的右边参数次方.数值型参数首先转换成通用类型. 结果类型是参数经强制规则转换后的结果;

若结果不能以该类型表达(计算整数的幂结果为负数, 或计算负浮点数的幂为无效的值), 则引发一个TypeError 异常.

With mixed operand types, the coercion rules for binary

arithmetic operators apply. For int and long int operands, the

result has the same type as the operands (after coercion) unless

the second argument is negative; in that case, all arguments are

converted to float and a float result is delivered. For example,

10**2 returns 100, but

10**-2 returns 0.01. (This last feature

was added in Python 2.2. In Python 2.1 and before, if both

arguments were of integer types and the second argument was

negative, an exception was raised).

Raising 0.0 to a negative power results in a

ZeroDivisionError. Raising a negative

number to a fractional power results in a ValueError.

5.5 一元算术运算符 Unary arithmetic operations

All unary arithmetic (and bit-wise) operations have the same

priority:

所有一元算术运算符(和位运算符)有相同的优先级:

The unary - (minus) operator yields the negation of

its numeric argument.

一元运算符-(减)对其数值型操作数取负.

The unary + (plus) operator yields its numeric

argument unchanged.

一元运算符+(加)不改变其数值型操作数.

The unary ~ (invert) operator yields the bit-wise

inversion of its plain or long integer argument. The bit-wise

inversion of x is defined as -(x+1). It

only applies to integral numbers.

一元运算符 (取反)对其普通整数或长整数参数求逆(比特级).x的比特级求逆运算定义为-(x+1).它仅仅用于整数型的操作数.

In all three cases, if the argument does not have the proper

type, a TypeError exception is raised.

在以上所有的三种情况下, 如果参数的类型不合法, 就会引发一个TypeError异常.

你可能感兴趣的:(python语言的幂运算符)