python算术运算符_Python算术运算符

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,

以下是算术运算符,

OperatorNameDescriptions
+Addition OperatorIt returns the addition of two expressions.
-Subtraction OperatorIt returns the subtraction of two expressions.
*Multiplication OperatorIt returns the product of two expressions.
**Power OperatorIt returns the value of the expression raised to the given power i.e. it returns the expression1 raised to the power of expression2.
/Division OperatorIt returns the quotient of two expressions.
//Floor Division OperatorIt returns the integral part of the quotient of two expressions.
%Modulus OperatorIt 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算术运算符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值