如何在Python 3 中使用运算符进行数学运算?
Python3 为我们提供了整数和浮点数等数据类型以及各种运算符,以执行用于图形绘制、机器学习算法、统计、数据分析目的的数学计算。运算符是执行特定操作的符号,如果经常处理数字,这非常有用。运算符优先级指定当两个或多个具有不同优先级的运算符在表达式中相邻时计算运算符符的顺序。下面是一元运算符和算术运算符的快速参考表。
Python中的运算符
Type | Operator | Name | Example | Description |
---|---|---|---|---|
Urinary Operator | – | Minus | – x | Negates the numeric argument |
+ | Plus | + x | numeric argument unchanged. | |
~ | Invert | – (x + 1) | bit-wise inversion of x | |
Arithmetic Operator | + | Addition | x + y | Add two operands using ‘+’ operator in between |
– | Subtraction | x – y | subtract two operands using the ‘-‘ operator in between | |
* | Multiplication | x * y | Multiply two operands using ‘*’ operator in between | |
/ | Division | x / y | Divide left operand with right operand using ‘/’ operator | |
// | Floor Division | x // y | Divide left operand with right operand using ‘//’ operator and provide the only quotient as an output | |
** | Exponentiation | x ** y | Exponentiation (power) x to the power y | |
% | Modulo | x % y | Divide left operand with right operand using ‘%’ operator and provide the only remainder as an output |
运算符优先级
Operator | Meaning | Associativity |
---|---|---|
** | Exponent | Right-to-left |
~x | Bitwise NOT (Invert) | Left-right |
+x, -x | Unary plus, Unary minus | Left-right |
*, /, //, % | Multiplication, Division, Floor division, Modulus | Left-right |
+, – | Addition, Subtraction | Left-right |
示例 1:一元运算符。
Python3
a = 2.202
b = -2.202
# This inverts the sign
# for both integer as
# well as float type
c = -a
print("Minus operator value 1:", c)
c = -b
print("Minus operator value 2:", c)
# This does not inverts the sign
# for both integer as well
# as float type
c = +a
print("Plus operator value 1:", c)
c = +b
print("Plus operator value 2:", c)
a = 2
b = -2
# This inverts the sign only
# for integer type as perform
# operation as per this '-(x + 1)'.
c = ~a # -(2 + 1)
print("Invert operator value 1:", c)
c = ~b # -(-2 + 1)
print("Invert operator value 2:", c)
Python3
a = 4
b = -5
# This + operator performs
# addition of two operands
# or numbers
d = a + b
print("Addition value 1:", d)
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
d = []
for j in range(len(a)):
d.append(a[j] + b[j])
print("Addition value 2:", d)
Python3
a = 4
b = -5
# This - operator performs
# subtraction of two operands
# or numbers
d = a - b
print("Subtraction value 1:",d)
a = [ 1 ,4,5]
b = [1, 2, 3]
print("Subtraction values:")
for i in range(len(a)) :
print(a[i] - b[i])
Python3
a = 4
b = -5
c = 5.02
# This * operator performs
# Multiplication of two
# operands or numbers
d = a * b
print("Multiplication value 1:", d)
d = a * c
print("Multiplication value 2:", d)
Python3
a = 20
b = -5
c = 5.02
# This '/' operator performs
# Division of two operands
# or numbers
d = a / b
print("Division value 1:", d)
d = a / c
print("Division value 2:", d)
Python3
a = 20
b = -5
c = 5.02
# This // operator performs
# Floor Division of two
# operands or numbers
d = a // b
print("Floor Division value 1:", d)
d = a // c
print("Floor Division value 2:", d)
Python3
a = 5
b = 3
c = -3
# This ** operator performs
# Exponential operation of two
# operands or numbers
d = a ** b
print("Exponent value 1:", d)
d = a ** c
print("Exponent value 2:", d)
Python3
a = 12
b = 5
c = 3
# This % operator performs Modulus
# of two operands or numbers and
# return the remainder
d = a % b
print("Modulus value 1:", d)
d = c % b
print("Modulus value 2:", d)
输出:
Minus operator value 1: -2.202
Minus operator value 2: 2.202
Plus operator value 1: 2.202
Plus operator value 2: -2.202
Invert operator value 1: -3
Invert operator value 2: 1
示例 2:加法运算符。
Python3
a = 4
b = -5
# This + operator performs
# addition of two operands
# or numbers
d = a + b
print("Addition value 1:", d)
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
d = []
for j in range(len(a)):
d.append(a[j] + b[j])
print("Addition value 2:", d)
输出:
Addition value 1: -1
Addition value 2: [7, 9, 11, 13, 15]
示例 3:减法运算符。
Python3
a = 4
b = -5
# This - operator performs
# subtraction of two operands
# or numbers
d = a - b
print("Subtraction value 1:",d)
a = [ 1 ,4,5]
b = [1, 2, 3]
print("Subtraction values:")
for i in range(len(a)) :
print(a[i] - b[i])
输出:
Subtraction value 1: 9
Subtraction values:
0
2
2
示例 4:乘法运算符。
Python3
a = 4
b = -5
c = 5.02
# This * operator performs
# Multiplication of two
# operands or numbers
d = a * b
print("Multiplication value 1:", d)
d = a * c
print("Multiplication value 2:", d)
输出
Multiplication value 1: -20
Multiplication value 2: 20.08
示例 5:除法运算符。
Python3
a = 20
b = -5
c = 5.02
# This '/' operator performs
# Division of two operands
# or numbers
d = a / b
print("Division value 1:", d)
d = a / c
print("Division value 2:", d)
输出:
Division value 1: -4.0
Division value 2: 3.9840637450199208
示例 6:楼层除法运算符。
Python3
a = 20
b = -5
c = 5.02
# This // operator performs
# Floor Division of two
# operands or numbers
d = a // b
print("Floor Division value 1:", d)
d = a // c
print("Floor Division value 2:", d)
输出:
Floor Division value 1: -4
Floor Division value 2: 3.0
示例 7:指数运算符。
Python3
a = 5
b = 3
c = -3
# This ** operator performs
# Exponential operation of two
# operands or numbers
d = a ** b
print("Exponent value 1:", d)
d = a ** c
print("Exponent value 2:", d)
输出:
Exponent value 1: 125
Exponent value 2: 0.008
示例 8:模运算符。
Python3
a = 12
b = 5
c = 3
# This % operator performs Modulus
# of two operands or numbers and
# return the remainder
d = a % b
print("Modulus value 1:", d)
d = c % b
print("Modulus value 2:", d)
输出
Modulus value 1: 2
Modulus value 2: 3