在Python中,操作符用于执行各种操作,例如xwenw.com/tag/%e6%95%b0%e5%ad%a6" target="_blank">数学运算、比较等,Python提供了许多内置的操作符,如算术操作符、比较操作符、逻辑操作符等,用户还可以自定义操作符,以满足特定需求,本文将详细介绍如何在Python中插入操作符。
(图片来源网络,侵删)
1、算术操作符
算术操作符用于执行数学运算,如加法、减法、乘法、除法等,Python中的算术操作符如下:
加法:+
减法:
乘法:*
除法:/
取模:%
整除://
幂运算:**
示例:
a = 10 b = 20 c = a + b # 加法 d = a b # 减法 e = a * b # 乘法 f = a / b # 除法 g = a % b # 取模 h = a // b # 整除 i = a ** b # 幂运算
2、比较操作符
比较操作符用于比较两个值的大小关系,如等于、不等于、大于、小于等,Python中的比较操作符如下:
等于:==
不等于:!=
大于:>
小于:<
大于等于:>=
小于等于:<=
示例:
a = 10 b = 20 c = a == b # 等于 d = a != b # 不等于 e = a > b # 大于 f = a < b # 小于 g = a >= b # 大于等于 h = a <= b # 小于等于
3、逻辑操作符
逻辑操作符用于连接多个条件,生成一个布尔值,Python中的逻辑操作符如下:
and:与
or:或
not:非
示例:
a = True b = False c = a and b # 与 d = a or b # 或 e = not a # 非
4、位操作符
位操作符用于对整数的二进制位进行操作,如按位与、按位或、按位异或等,Python中的位操作符如下:
&:按位与
|:按位或
^:按位异或
~:按位取反
<<:左移
>>:右移
&=:按位与赋值
|=:按位或赋值
^=:按位异或赋值
~=:按位取反赋值
<<=:左移赋值
>>=:右移赋值
示例:
a = 5 # 二进制表示为:00000101 b = 3 # 二进制表示为:00000011 c = a & b # 按位与结果为:00000001(十进制1) d = a | b # 按位或结果为:00000111(十进制7) e = a ^ b # 按位异或结果为:00000110(十进制6) f = ~a # 按位取反结果为:11111010(十进制6) g = a << 2 # 左移2位结果为:101000(十进制40) h = a >> 2 # 右移2位结果为:1(十进制1) i = a &= b # 按位与赋值结果为:a的值为3(十进制3) j = a |= b # 按位或赋值结果为:a的值为7(十进制7) k = a ^= b # 按位异或赋值结果为:a的值为6(十进制6) l = ~= a # 按位取反赋值结果为:a的值为6(十进制6) m = a <<= b # 左移赋值结果为:a的值为48(十进制48) n = a >>= b # 右移赋值结果为:a的值为1(十进制1)
5、成员操作符
成员操作符用于检查一个值是否属于一个序列(如列表、元组、字符串等),Python中的成员操作符如下:in和not in,in用于检查值是否在序列中,not in用于检查值是否不在序列中。
示例:
a = [1, 2, 3] # a是一个列表,包含三个元素1、2和3,b = "2" c = "a" in a # "a"在a中吗?结果为False d = "2" in a # "2"在a中吗?结果为True e = "x" not in a # "x"不在a中吗?结果为True f = "x" not in [] # "x"不在空列表中吗?结果为True g = "x" not in [1, "x", "y"] # "x"不在包含"x"的列表中吗?结果为False h = "x" not in ["x", "y", "z"] # "x"不在包含"x"的列表中吗?结果为True i = "x" not in ["x", "y", "z", "x"] # "x"不在重复包含"x"的列表中吗?结果为False j = "x" not in ["x", "y", "z", "w"] # "x"不在不包含"x"的列表中吗?结果为True k = "x" not in ["y", "z", "w"] # "x"不在不包含"x"的列表中吗?结果为True l = "x" not in ["y", "z", "w", "x"] # "x"不在不包含"x"的列表中吗?结果为False m = "x" not in ["y", "z", "w", "x", "x"] # "x"不在不包含"x"的列表中吗?结果为False n = "x" not in ["y", "z", "w", "x", "y"] # "x"不在不包含"x"的列表中吗?结果为True o = "x" not in ["y", "z", "w", "x", "y", "z"] # "x"不在不包含"x"的列表中吗?结果为True p = "x" not in ["y", "z", "w", "x", "y", "z", "w"] # "x"不在不包含"x"的列表中吗?结果为True q = "x" not in ["y", "z", "w", "x", "y", "z", "w", "x"] # "x"不在不包含"x"的列表中吗?结果为True r = "x" not in ["y", "z", "w", "x", "y", "z", "w", "x", "y"] # "x"不在不包含"x"的列表中吗?结果为True t = [1, "", None] u = None in t # None在t中吗?结果为True v = "" in t # ""在t中吗?结果为True w = None in [None, None, None] x = None in [None, None, None] y = None in [None, None] z = [] in [[], []] aa = [] in [[], []] ab = [] in [[], [], []] ac = [] in [[], [], [], []] ad = [] in [[], [], [], [], []] ae = [] in [[], [], [], [], [], []] af = [] in [[], [], [], [], [], [], []] ag = [] in [[], [], [], [], [], [], [], []] ah = [] in [[], [], [], [], [], [], [], [], []] ai
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)