Python 简明教程
Python - Arithmetic Operators
Python Arithmetic Operators
算术运算符是二元运算符,意思是对两个操作数进行运算。Python 完全支持混合算术。也就是说,两个操作数可以是两种不同的数字类型。在这种情况下,Python 会扩展较窄的操作数。整数对象比浮点数窄,而浮点数比复数对象窄。因此,int 和 float 算术运算的结果是 float。float 和 complex 的结果是复数,类似地,integer 和 complex 对象上的运算结果是 complex 对象。
Arithmetic operators are binary operators in the sense they operate on two operands. Python fully supports mixed arithmetic. That is, the two operands can be of two different number types. In such a situation, Python widens the narrower of the operands. An integer object is narrower than float object, and float is narrower than complex object. Hence, the result of arithmetic operation of int and a float is a float. Result of float and a complex is a complex number, similarly, operation on an integer and a complex object results in a complex object.
Different Arithmetic Operators in Python
以下是列出 Python 中所有可用算术运算符的表格:
Following is the table which lists down all the arithmetic operators available in Python:
Operator |
Name |
Example |
+ |
Addition |
a + b = 30 |
- |
Subtraction |
a – b = -10 |
* |
Multiplication |
a * b = 200 |
/ |
Division |
b / a = 2 |
% |
Modulus |
b % a = 0 |
** |
Exponent |
a*b =10*20 |
// |
Floor Division |
9//2 = 4 |
让我们通过示例来研究这些运算符。
Let us study these operators with examples.
Addition Operator (+)
此运算符发音为 plus,是基本的算术运算符。它将两侧的两个数字操作数相加,并返回加法结果。
This operator pronounced as plus, is a basic arithmetic operator. It adds the two numeric operands on the either side and returns the addition result.
在以下示例中,两个整数 variables 是“+”运算符的操作数。
In the following example, the two integer variables are the operands for the "+" operator.
a=10
b=20
print ("Addition of two integers")
print ("a =",a,"b =",b,"addition =",a+b)
它将生成以下 output −
It will produce the following output −
Addition of two integers
a = 10 b = 20 addition = 30
整型和浮点数的加法得到浮点数。
Addition of integer and float results in a float.
a=10
b=20.5
print ("Addition of integer and float")
print ("a =",a,"b =",b,"addition =",a+b)
它将生成以下 output −
It will produce the following output −
Addition of integer and float
a = 10 b = 20.5 addition = 30.5
将浮点数加到复数的结果是复数。
The result of adding float to complex is a complex number.
a=10+5j
b=20.5
print ("Addition of complex and float")
print ("a=",a,"b=",b,"addition=",a+b)
它将生成以下 output −
It will produce the following output −
Addition of complex and float
a= (10+5j) b= 20.5 addition= (30.5+5j)
Subtraction Operator (-)
此运算符称为负号,计算第二个操作数与第一个操作数之差。如果第二个操作数较大,则结果为负数。
This operator, known as minus, subtracts the second operand from the first. The resultant number is negative if the second operand is larger.
第一个示例说明两个整数的减法。
First example shows subtraction of two integers.
a=10
b=20
print ("Subtraction of two integers:")
print ("a =",a,"b =",b,"a-b =",a-b)
print ("a =",a,"b =",b,"b-a =",b-a)
结果 −
Result −
Subtraction of two integers
a = 10 b = 20 a-b = -10
a = 10 b = 20 b-a = 10
用一个整数和一个浮点数进行减法遵循相同的原理。
Subtraction of an integer and a float follows the same principle.
a=10
b=20.5
print ("subtraction of integer and float")
print ("a=",a,"b=",b,"a-b=",a-b)
print ("a=",a,"b=",b,"b-a=",b-a)
它将生成以下 output −
It will produce the following output −
subtraction of integer and float
a= 10 b= 20.5 a-b= -10.5
a= 10 b= 20.5 b-a= 10.5
进行复杂减法和浮点数减法时,实部参与运算。
In the subtraction involving a complex and a float, real component is involved in the operation.
a=10+5j
b=20.5
print ("subtraction of complex and float")
print ("a=",a,"b=",b,"a-b=",a-b)
print ("a=",a,"b=",b,"b-a=",b-a)
它将生成以下 output −
It will produce the following output −
subtraction of complex and float
a= (10+5j) b= 20.5 a-b= (-10.5+5j)
a= (10+5j) b= 20.5 b-a= (10.5-5j)
Multiplication Operator (*)
*(星号)符号在 Python 中定义为乘法运算符(如同在许多语言中定义的一样)。它返回其两侧两个操作数的乘积。如果任一操作数为负数,结果也为负数。如果两者均为负数,结果为正数。更改操作数的顺序不会更改结果
The * (asterisk) symbol is defined as a multiplication operator in Python (as in many languages). It returns the product of the two operands on its either side. If any of the operands negative, the result is also negative. If both are negative, the result is positive. Changing the order of operands doesn’t change the result
a=10
b=20
print ("Multiplication of two integers")
print ("a =",a,"b =",b,"a*b =",a*b)
它将生成以下 output −
It will produce the following output −
Multiplication of two integers
a = 10 b = 20 a*b = 200
在乘法中,浮点数操作数可能采用标准小数点表示法,或采用科学计数法。
In multiplication, a float operand may have a standard decimal point notation, or a scientific notation.
a=10
b=20.5
print ("Multiplication of integer and float")
print ("a=",a,"b=",b,"a*b=",a*b)
a=-5.55
b=6.75E-3
print ("Multiplication of float and float")
print ("a =",a,"b =",b,"a*b =",a*b)
它将生成以下 output −
It will produce the following output −
Multiplication of integer and float
a = 10 b = 20.5 a-b = -10.5
Multiplication of float and float
a = -5.55 b = 0.00675 a*b = -0.037462499999999996
对于涉及一个复杂操作数的乘法运算,另一个操作数与实部和虚部两部分相乘。
For the multiplication operation involving one complex operand, the other operand multiplies both the real part and imaginary part.
a=10+5j
b=20.5
print ("Multiplication of complex and float")
print ("a =",a,"b =",b,"a*b =",a*b)
它将生成以下 output −
It will produce the following output −
Multiplication of complex and float
a = (10+5j) b = 20.5 a*b = (205+102.5j)
Division Operator (/)
“/”符号通常称为正斜杠。除法运算符的结果为分子(左操作数)除以分母(右操作数)。如果任一操作数为负数,则结果为负数。由于不能在内存中存储无限大,因此在分母为 0 时,Python 会引发 ZeroDivisionError。
The "/" symbol is usually called as forward slash. The result of division operator is numerator (left operand) divided by denominator (right operand). The resultant number is negative if any of the operands is negative. Since infinity cannot be stored in the memory, Python raises ZeroDivisionError if the denominator is 0.
在 Python 中,除法运算符的结果始终是浮点数,即使两个操作数都是整数。
The result of division operator in Python is always a float, even if both operands are integers.
a=10
b=20
print ("Division of two integers")
print ("a=",a,"b=",b,"a/b=",a/b)
print ("a=",a,"b=",b,"b/a=",b/a)
它将生成以下 output −
It will produce the following output −
Division of two integers
a= 10 b= 20 a/b= 0.5
a= 10 b= 20 b/a= 2.0
除法中,浮点数操作数可能采用标准小数点表示法,或采用科学计数法。
In Division, a float operand may have a standard decimal point notation, or a scientific notation.
a=10
b=-20.5
print ("Division of integer and float")
print ("a=",a,"b=",b,"a/b=",a/b)
a=-2.50
b=1.25E2
print ("Division of float and float")
print ("a=",a,"b=",b,"a/b=",a/b)
它将生成以下 output −
It will produce the following output −
Division of integer and float
a= 10 b= -20.5 a/b= -0.4878048780487805
Division of float and float
a= -2.5 b= 125.0 a/b= -0.02
当其中一个操作数是复数时,其他操作数与复数(实数和虚数)对象的各个部分进行除法。
When one of the operands is a complex number, division between the other operand and both parts of complex number (real and imaginary) object takes place.
a=7.5+7.5j
b=2.5
print ("Division of complex and float")
print ("a =",a,"b =",b,"a/b =",a/b)
print ("a =",a,"b =",b,"b/a =",b/a)
它将生成以下 output −
It will produce the following output −
Division of complex and float
a = (7.5+7.5j) b = 2.5 a/b = (3+3j)
a = (7.5+7.5j) b = 2.5 b/a = (0.16666666666666666-0.16666666666666666j)
如果分子为 0,则除法结果始终为 0,但分母为 0 除外,在这种情况之下,Python 会引发 ZeroDivisionError,并显示除以零错误消息。
If the numerator is 0, the result of division is always 0 except when denominator is 0, in which case, Python raises ZeroDivisionError wirh Division by Zero error message.
a=0
b=2.5
print ("a=",a,"b=",b,"a/b=",a/b)
print ("a=",a,"b=",b,"b/a=",b/a)
它将生成以下 output −
It will produce the following output −
a= 0 b= 2.5 a/b= 0.0
Traceback (most recent call last):
File "C:\Users\mlath\examples\example.py", line 20, in <module>
print ("a=",a,"b=",b,"b/a=",b/a)
~^~
ZeroDivisionError: float division by zero
Modulus Operator (%)
Python 定义了符号“%”,这被称为百分比符号,即取模(或 modulo)运算符。它返回分母除以分子后的余数。它也可以称为取余运算符。取模运算符的结果是整数商之后的剩余数字。举个例子,当 10 除以 3 时,商为 3,余数为 1。因此,10%3(通常读作 10 模 3)的结果为 1。
Python defines the "%" symbol, which is known aa Percent symbol, as Modulus (or modulo) operator. It returns the remainder after the denominator divides the numerator. It can also be called Remainder operator. The result of the modulus operator is the number that remains after the integer quotient. To give an example, when 10 is divided by 3, the quotient is 3 and remainder is 1. Hence, 10%3 (normally pronounced as 10 mod 3) results in 1.
如果两个操作数都是整数,则模值是整数。如果分子完全可除,则余数为 0。如果分子小于分母,则模数等于分子。如果分母为 0,则 Python 会引发 ZeroDivisionError。
If both the operands are integer, the modulus value is an integer. If numerator is completely divisible, remainder is 0. If numerator is smaller than denominator, modulus is equal to the numerator. If denominator is 0, Python raises ZeroDivisionError.
a=10
b=2
print ("a=",a, "b=",b, "a%b=", a%b)
a=10
b=4
print ("a=",a, "b=",b, "a%b=", a%b)
print ("a=",a, "b=",b, "b%a=", b%a)
a=0
b=10
print ("a=",a, "b=",b, "a%b=", a%b)
print ("a=", a, "b=", b, "b%a=",b%a)
它将生成以下 output −
It will produce the following output −
a= 10 b= 2 a%b= 0
a= 10 b= 4 a%b= 2
a= 10 b= 4 b%a= 4
a= 0 b= 10 a%b= 0
Traceback (most recent call last):
File "C:\Users\mlath\examples\example.py", line 13, in <module>
print ("a=", a, "b=", b, "b%a=",b%a)
~^~
ZeroDivisionError: integer modulo by zero
如果任何操作数为浮点数,则模值始终为浮点数。
If any of the operands is a float, the mod value is always float.
a=10
b=2.5
print ("a=",a, "b=",b, "a%b=", a%b)
a=10
b=1.5
print ("a=",a, "b=",b, "a%b=", a%b)
a=7.7
b=2.5
print ("a=",a, "b=",b, "a%b=", a%b)
a=12.4
b=3
print ("a=",a, "b=",b, "a%b=", a%b)
它将生成以下 output −
It will produce the following output −
a= 10 b= 2.5 a%b= 0.0
a= 10 b= 1.5 a%b= 1.0
a= 7.7 b= 2.5 a%b= 0.20000000000000018
a= 12.4 b= 3 a%b= 0.40000000000000036
Python 不支持将复数用作模运算中的操作数。它会引发 TypeError: % 的不兼容操作数类型。
Python doesn’t accept complex numbers to be used as operand in modulus operation. It throws TypeError: unsupported operand type(s) for %.
Exponent Operator (**)
Python 使用 *(双星号)作为指数运算符(有时称为求幂运算符)。因此,对于 a*b,您可以说 a 的 b 次方,甚至是 a 的 b 次方。
Python uses * (double asterisk) as the exponent operator (sometimes called raised to operator). So, for a*b, you say a raised to b, or even bth power of a.
如果在指数表达式中,两个操作数都是整数,结果也是整数。如果其中一个为浮点数,则结果为浮点数。同样,如果任一操作数是复数,指数运算符返回一个复数。
If in the exponentiation expression, both operands are integer, result is also an integer. In case either one is a float, the result is float. Similarly, if either one operand is complex number, exponent operator returns a complex number.
如果底数为 0,则结果为 0,如果指数为 0,则结果始终为 1。
If the base is 0, the result is 0, and if the index is 0 then the result is always 1.
a=10
b=2
print ("a=",a, "b=",b, "a**b=", a**b)
a=10
b=1.5
print ("a=",a, "b=",b, "a**b=", a**b)
a=7.7
b=2
print ("a=",a, "b=",b, "a**b=", a**b)
a=1+2j
b=4
print ("a=",a, "b=",b, "a**b=", a**b)
a=12.4
b=0
print ("a=",a, "b=",b, "a**b=", a**b)
print ("a=",a, "b=",b, "b**a=", b**a)
它将生成以下 output −
It will produce the following output −
a= 10 b= 2 a**b= 100
a= 10 b= 1.5 a**b= 31.622776601683793
a= 7.7 b= 2 a**b= 59.290000000000006
a= (1+2j) b= 4 a**b= (-7-24j)
a= 12.4 b= 0 a**b= 1.0
a= 12.4 b= 0 b**a= 0.0
Floor Division Operator (//)
取整除也称为整数除法。Python 使用 //(双正斜杠)符号来实现此目的。与返回余数的模数或模运算不同,取整除给出了涉及操作数的商。
Floor division is also called as integer division. Python uses // (double forward slash) symbol for the purpose. Unlike the modulus or modulo which returns the remainder, the floor division gives the quotient of the division of operands involved.
如果两个操作数都为正,取整运算符返回一个移除了小数部分的数字。例如,9.8 除以 2 的取整除结果为 4(纯除法为 4.9,去除掉小数部分,结果为 4)。
If both operands are positive, floor operator returns a number with fractional part removed from it. For example, the floor division of 9.8 by 2 returns 4 (pure division is 4.9, strip the fractional part, result is 4).
但如果其中一个操作数为负,结果会从 0 向远离的方向取整(朝向负无穷大)。-9.8 除以 2 的取整除结果为 5(纯除法为 4.9,从 0 向远离的方向取整)。
But if one of the operands is negative, the result is rounded away from zero (towards negative infinity). Floor division of -9.8 by 2 returns 5 (pure division is -4.9, rounded away from 0).
a=9
b=2
print ("a=",a, "b=",b, "a//b=", a//b)
a=9
b=-2
print ("a=",a, "b=",b, "a//b=", a//b)
a=10
b=1.5
print ("a=",a, "b=",b, "a//b=", a//b)
a=-10
b=1.5
print ("a=",a, "b=",b, "a//b=", a//b)
它将生成以下 output −
It will produce the following output −
a= 9 b= 2 a//b= 4
a= 9 b= -2 a//b= -5
a= 10 b= 1.5 a//b= 6.0
a= -10 b= 1.5 a//b= -7.0
Precedence and Associativity of Python Arithmetic Operators
Operator(s) |
Description |
Associativity |
** |
Exponent Operator |
Associativity of Exponent operator is from Right to Left. |
%, *, /, // |
Modulus, Multiplication, Division, and Floor Division |
Associativity of Modulus, Multiplication, Division, and Floor Division operators are from Left to Right. |
+, – |
Addition and Subtraction Operators |
Associativity of Addition and Subtraction operators are from Left to Right. |
下表显示了 Python 中算术运算符的优先级和结合性。
The following table shows the precedence and associativity of the arithmetic operators in Python.
Complex Number Arithmetic
当两个操作数都是复数对象时,算术运算的不同之处在于它们的行为。
Arithmetic operators behave slightly differently when the both operands are complex number objects.
复数的加法和减法是各自实数部分和虚数部分的简单加/减法。
Addition and subtraction of complex numbers is a simple addition/subtraction of respective real and imaginary components.
a=2.5+3.4j
b=-3+1.0j
print ("Addition of complex numbers - a=",a, "b=",b, "a+b=", a+b)
print ("Subtraction of complex numbers - a=",a, "b=",b, "a-b=", a-b)
它将生成以下 output −
It will produce the following output −
Addition of complex numbers - a= (2.5+3.4j) b= (-3+1j) a+b= (-0.5+4.4j)
Subtraction of complex numbers - a= (2.5+3.4j) b= (-3+1j) a-b= (5.5+2.4j)
复数的乘法类似于代数中的二项式乘法。如果“a+bj”和“x+yj”是两个复数,那么它们相乘的结果由该公式给出:
Multiplication of complex numbers is similar to multiplication of two binomials in algebra. If "a+bj" and "x+yj" are two complex numbers, then their multiplication is given by this formula −
(a+bj)*(x+yj) = ax+ayj+xbj+byj2 = (ax-by)+(ay+xb)j
For example,
For example,
a=6+4j
b=3+2j
c=a*b
c=(18-8)+(12+12)j
c=10+24j
下面的程序确认了该结果:
The following program confirms the result −
a=6+4j
b=3+2j
print ("Multplication of complex numbers - a=",a, "b=",b, "a*b=", a*b)
要了解如何对两个复数进行除法,我们应该利用复数的共轭。Python 的 complex 对象有一个 conjugate() 方法,它返回一个虚数部分符号相反的复数。
To understand the how the division of two complex numbers takes place, we should use the conjugate of a complex number. Python’s complex object has a conjugate() method that returns a complex number with the sign of imaginary part reversed.
>>> a=5+6j
>>> a.conjugate()
(5-6j)
要除两个复数,将分子和分母除以分母的共轭,然后进行乘法。
To divide two complex numbers, divide and multiply the numerator as well as the denominator with the conjugate of denominator.
a=6+4j
b=3+2j
c=a/b
c=(6+4j)/(3+2j)
c=(6+4j)*(3-2j)/3+2j)*(3-2j)
c=(18-12j+12j+8)/(9-6j+6j+4)
c=26/13
c=2+0j
要进行验证,运行以下代码:
To verify, run the following code −
a=6+4j
b=3+2j
print ("Division of complex numbers - a=",a, "b=",b, "a/b=", a/b)
Python 中的复杂类不支持取模运算符 (%) 和取整除运算符 (//)。
Complex class in Python doesn’t support the modulus operator (%) and floor division operator (//).