python算术运算符
Arithmetic operators are used to perform various arithmetic / mathematical operations and they are binary operators means they require two operands for the calculations.
算术运算符用于执行各种算术/数学运算,它们是二进制运算符,表示它们需要两个操作数才能进行计算。
Following are the arithmetic operators,
以下是算术运算符,
Operator | Name | Descriptions |
---|---|---|
+ | Addition Operator | It returns the addition of two expressions. |
- | Subtraction Operator | It returns the subtraction of two expressions. |
* | Multiplication Operator | It returns the product of two expressions. |
** | Power Operator | It returns the value of the expression raised to the given power i.e. it returns the expression1 raised to the power of expression2. |
/ | Division Operator | It returns the quotient of two expressions. |
// | Floor Division Operator | It returns the integral part of the quotient of two expressions. |
% | Modulus Operator | It returns the decimal part of the quotient i.e. remainder of the division of two expressions. |
操作员 | 名称 | 内容描述 |
---|---|---|
+ | 加法运算符 | 它返回两个表达式的加法。 |
-- | 减法运算符 | 它返回两个表达式的减法。 |
* | 乘法运算符 | 它返回两个表达式的乘积。 |
** | 电力运营商 | 它将表达式的值返回给定的幂,即,将表达式1返回给表达式2的幂。 |
/ | 部门运营商 | 它返回两个表达式的商。 |
// | 楼层划分操作员 | 它返回两个表达式的商的整数部分。 |
% | 模运算符 | 它返回商的小数部分,即两个表达式除法的余数。 |
Syntax:
句法:
exp1 + exp2
exp1 - exp2
exp1 * exp2
exp1 ** exp2
exp1 / exp2
exp1 // exp2
exp1 % exp2
演示算术运算符示例的程序 (Programs to demonstrate the example of arithmetic operators)
Example 1:
范例1:
# Python program to demonstrate
# the example of arithmetic operators
a = 10
b = 3
# printing the values
print("a:", a)
print("b:", b)
# operations
print("a + b :", a + b)
print("a - b :", a - b)
print("a * b :", a * b)
print("a ** b:", a ** b)
print("a / b :", a / b)
print("a // b:", a // b)
print("a % b :", a % b)
Output:
输出:
a: 10
b: 3
a + b : 13
a - b : 7
a * b : 30
a ** b: 1000
a / b : 3.3333333333333335
a // b: 3
a % b : 1
Example 2:
范例2:
# Python program to demonstrate
# the example of arithmetic operators
a = 10.10
b = -2.5
# printing the values
print("a:", a)
print("b:", b)
# operations
print("a + b :", a + b)
print("a - b :", a - b)
print("a * b :", a * b)
print("a ** b:", a ** b)
print("a / b :", a / b)
# // operator rounds the result
# down to the nearest whole number
print("a // b:", a // b)
print("a % b :", a % b)
Output:
输出:
a: 10.1
b: -2.5
a + b : 7.6
a - b : 12.6
a * b : -25.25
a ** b: 0.0030845837443758094
a / b : -4.04
a // b: -5.0
a % b : -2.4000000000000004
Example 3:
范例3:
# Python program to demonstrate
# the example of arithmetic operators
# Operations on lists, strings, etc
x = "Hello"
y = "World"
# '+', '*' with strings
print("x + y:", x + y)
print("x + y + x:", x + y + x)
print("x * 2:", x * 2)
print("x * 10:", x * 10)
print()
# '+', '*' with lists
x = [10, 20, 30, 40]
y = [40, 30, 20, 10]
print("x + y:", x + y)
print("x + y + x:", x + y + x)
print("x * 2:", x * 2)
print("x * 3:", x * 3)
print()
Output:
输出:
x + y: HelloWorld
x + y + x: HelloWorldHello
x * 2: HelloHello
x * 10: HelloHelloHelloHelloHelloHelloHelloHelloHelloHello
x + y: [10, 20, 30, 40, 40, 30, 20, 10]
x + y + x: [10, 20, 30, 40, 40, 30, 20, 10, 10, 20, 30, 40]
x * 2: [10, 20, 30, 40, 10, 20, 30, 40]
x * 3: [10, 20, 30, 40, 10, 20, 30, 40, 10, 20, 30, 40]
翻译自: https://www.includehelp.com/python/arithmetic-operators.aspx
python算术运算符