Python运算符
Posted evilsnake
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python运算符相关的知识,希望对你有一定的参考价值。
1:算术运算符
+:加 两个对象相加 a + b
-:减 得到负数或是一个数减去另一个数
*:乘 两个数相乘或是返回一个被重复若干次的字符串
/:除 x除以y
%:取模 返回除法的余数
**:幂 返回x的y次幂
//:取整除 返回商的整数部分
a = 21 b = 10 c = 0 c = a + b print("a = ", a, "b = ", b, "a+b = ", c) c = a - b print("a = ", a, "b = ", b, "a-b = ", c) c = a * b print("a = ", a, "b = ", b, "a*b = ", c) c = a / b print("a = ", a, "b = ", b, "a/b = ", c) c = a % b print("a = ", a, "b = ", b, "a%b = ", c) a = 2 b = 3 c = a ** b print("a = ", a, "b = ", b, "a**b = ", c) a = 9 b = 4 c = a // b print("a = ", a, "b = ", b, "a//b = ", c) print("1" + "2") print([1, 2] + [3, 4])
输出结果为
a = 21 b = 10 a+b = 31
a = 21 b = 10 a-b = 11
a = 21 b = 10 a*b = 210
a = 21 b = 10 a/b = 2.1
a = 21 b = 10 a%b = 1
a = 2 b = 3 a**b = 8
a = 9 b = 4 a//b = 2
12
[1, 2, 3, 4]
2:赋值运算符
=: 简单的赋值运算符 c = a + b 将 a + b 的运算结果赋值为 c
+=: 加法赋值运算符 c += a 等效于 c = c + a
-=: 减法赋值运算符 c -= a 等效于 c = c - a
*=: 乘法赋值运算符 c *= a 等效于 c = c * a
/=: 除法赋值运算符 c /= a 等效于 c = c / a
%=: 取模赋值运算符 c %= a 等效于 c = c % a
**=: 幂赋值运算符 c **= a 等效于 c = c ** a
//=: 取整除赋值运算符 c //= a 等效于 c = c // a
a = 21 b = 10 c = 0 # 赋值运算符 c = a + b print("c = ", c) # 加法赋值运算符 c += a print("c = ", c) # 减法赋值运算符 c -= a print("c = ", c) # 乘法赋值运算符 c *= a print("c = ", c) # 除法赋值运算符 c /= a print("c = ", c) # 取模赋值运算符 c = 2 c %= a print("c = ", c) # 幂赋值运算符 a = 3 c **= a print("c = ", c) # 取整除赋值运算符 c //= a print("c = ", c)
输出结果为
c = 31
c = 52
c = 31
c = 651
c = 31.0
c = 2
c = 8
c = 2
3:比较运算符
==: 等于 比较对象是否相等 (a == b)
!=: 不等于 比较两个对象是否不相等 (a != b)
<>: 不等于 比较两个对象是否不相等 (a <> b) 这个运算符类似 != 。(python2.x版本支持,python3.x版本支持,为了方便统一使用!=)
>: 大于 返回x是否大于y (a > b)
<: 小于 返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。 (a < b)
>=: 大于等于 返回x是否大于等于y。 (a >= b)
<=: 小于等于 返回x是否小于等于y。 (a <= b)
is: 是判断两个标识符是不是引用自一个对象 x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False
链状比较运算符 例如 10 < a <=66
a = 20 b = 10 c = 0 print("a == b ", a == b) print("a != b ", a != b) # print(a <> b) 在python3.x版本下会报错的 print("a > b ", a > b) print("a < b ", a < b) b = 20 print("a >= b ", a >= b) print("a <= b ", a <= b) # is是比对唯一标识的 print("id(a) = ", id(a)) print("id(b) = ", id(b)) print("a is b ", a is b) a = [1] b = [1] print("a == b ", a == b) print(" a is b ", a is b) print("id(a) = ", id(a)) print("id(b) = ", id(b)) a = 20 b = 10 print(10 < a < 25)
输出结果:
a == b False
a != b True
a > b True
a < b False
a >= b True
a <= b True
id(a) = 1535632448
id(b) = 1535632448
a is b True
a == b True
a is b False
id(a) = 5501136
id(b) = 4805976
True
4:逻辑运算符
以下假设变量 a 为 10, b为 20:
and: x and y 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 (a and b) 返回 20。
or: x or y 布尔"或" - 如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值。 (a or b) 返回 10。
not: not x 布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。 not(a and b) 返回 False
1 b = True 2 print(not b) 3 4 5 a = False 6 print(a and b) 7 8 9 print(a or b) 10 11 12 a = 10 13 b = 20 14 print(a and b) 15 16 print(a or b) 17 18 a = 0 19 print(a or b) 20 21 22 print(not b)
输出结果:
False
False
True
20
10
20
False
以上是关于Python运算符的主要内容,如果未能解决你的问题,请参考以下文章