小白教程
所有教程
关于
Search
172.70.127.153
172.70.127.153
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 operator 模块”的源代码
本页内容
上一节:
Python3_pip
下一节:
Python_math_模块
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Python3 operator}}[[Category:Python 教程|49]] = Python3 operator 模块 = Python2.x 版本中,使用 [https://www.xiaobai.wang/python/func-number-cmp.html cmp()] 函数来比较两个列表、数字或字符串等的大小关系。 Python 3.X 的版本中已经没有 cmp() 函数,如果你需要实现比较功能,需要引入 operator 模块,适合任何对象,包含的方法有: == operator 模块包含的方法 == <sample title="" desc="" lang="python" hererun="1"> operator.lt(a, b) operator.le(a, b) operator.eq(a, b) operator.ne(a, b) operator.ge(a, b) operator.gt(a, b) operator.__lt__(a, b) operator.__le__(a, b) operator.__eq__(a, b) operator.__ne__(a, b) operator.__ge__(a, b) operator.__gt__(a, b) </sample> '''operator.lt(a, b)''' 与 '''a &lt b''' 相同, '''operator.le(a, b)''' 与 '''a <= b''' 相同,'''operator.eq(a, b)''' 与 '''a == b''' 相同,'''operator.ne(a, b)''' 与 '''a != b''' 相同,'''operator.gt(a, b)''' 与 '''a > b''' 相同,'''operator.ge(a, b)''' 与 '''a >= b''' 相同。 <sample title="" desc="" lang="python" hererun="1"> # 导入 operator 模块 import operator # 数字 x = 10 y = 20 print("x:",x, ", y:",y) print("operator.lt(x,y): ", operator.lt(x,y)) print("operator.gt(y,x): ", operator.gt(y,x)) print("operator.eq(x,x): ", operator.eq(x,x)) print("operator.ne(y,y): ", operator.ne(y,y)) print("operator.le(x,y): ", operator.le(x,y)) print("operator.ge(y,x): ", operator.ge(y,x)) print() # 字符串 x = "Google" y = "xiaoBai" print("x:",x, ", y:",y) print("operator.lt(x,y): ", operator.lt(x,y)) print("operator.gt(y,x): ", operator.gt(y,x)) print("operator.eq(x,x): ", operator.eq(x,x)) print("operator.ne(y,y): ", operator.ne(y,y)) print("operator.le(x,y): ", operator.le(x,y)) print("operator.ge(y,x): ", operator.ge(y,x)) print() # 查看返回值 print("type((operator.lt(x,y)): ", type(operator.lt(x,y))) </sample> 以上代码输出结果为: <sample title="" desc="" lang="python" hererun="1"> x: 10 , y: 20 operator.lt(x,y): True operator.gt(y,x): True operator.eq(x,x): True operator.ne(y,y): False operator.le(x,y): True operator.ge(y,x): True x: Google , y: xiaoBai operator.lt(x,y): True operator.gt(y,x): True operator.eq(x,x): True operator.ne(y,y): False operator.le(x,y): True operator.ge(y,x): True </sample> 比较两个列表: <sample title="" desc="" lang="python" hererun="1"> # 导入 operator 模块 import operator a = [1, 2] b = [2, 3] c = [2, 3] print("operator.eq(a,b): ", operator.eq(a,b)) print("operator.eq(c,b): ", operator.eq(c,b)) </sample> 以上代码输出结果为: <sample title="" desc="" lang="python" hererun="1"> operator.eq(a,b): False operator.eq(c,b): True </sample> === 运算符函数 === operator 模块提供了一套与 Python 的内置运算符对应的高效率函数。例如,'''operator.add(x, y)''' 与表达式 '''x+y''' 相同。 函数包含的种类有:对象的比较运算、逻辑运算、数学运算以及序列运算。 对象比较函数适用于所有的对象,函数名根据它们对应的比较运算符命名。 许多函数名与特殊方法名相同,只是没有双下划线。为了向后兼容性,也保留了许多包含双下划线的函数,为了表述清楚,建议使用没有双下划线的函数。 <sample title="" desc="" lang="python" hererun="1"> # Python 实例 # add(), sub(), mul() # 导入 operator 模块 import operator # 初始化变量 a = 4 b = 3 # 使用 add() 让两个值相加 print ("add() 运算结果 :",end=""); print (operator.add(a, b)) # 使用 sub() 让两个值相减 print ("sub() 运算结果 :",end=""); print (operator.sub(a, b)) # 使用 mul() 让两个值相乘 print ("mul() 运算结果 :",end=""); print (operator.mul(a, b)) </sample> 以上代码输出结果为: <sample title="" desc="" lang="python" hererun="1"> add() 运算结果 :7 sub() 运算结果 :1 mul() 运算结果 :12 </sample> {| class="table table-striped table-hover" ! 运算 ! 语法 ! 函数 |- | 加法 | <code>a + b</code> | <code>add(a, b)</code> |- | 字符串拼接 | <code>seq1 + seq2</code> | <code>concat(seq1, seq2)</code> |- | 包含测试 | <code>obj in seq</code> | <code>contains(seq, obj)</code> |- | 除法 | <code>a / b</code> | <code>truediv(a, b)</code> |- | 除法 | <code>a // b</code> | <code>floordiv(a, b)</code> |- | 按位与 | <code>a & b</code> | <code>and_(a, b)</code> |- | 按位异或 | <code>a ^ b</code> | <code>xor(a, b)</code> |- | 按位取反 | <code>~ a</code> | <code>invert(a)</code> |- | 按位或 | <code>a | b</code> | <code>or_(a, b)</code> |- | 取幂 | <code>a ** b</code> | <code>pow(a, b)</code> |- | 标识 | <code>a is b</code> | <code>is_(a, b)</code> |- | 标识 | <code>a is not b</code> | <code>is_not(a, b)</code> |- | 索引赋值 | <code>obj[k] = v</code> | <code>setitem(obj, k, v)</code> |- | 索引删除 | <code>del obj[k]</code> | <code>delitem(obj, k)</code> |- | 索引取值 | <code>obj[k]</code> | <code>getitem(obj, k)</code> |- | 左移 | <code>a << b</code> | <code>lshift(a, b)</code> |- | 取模 | <code>a % b</code> | <code>mod(a, b)</code> |- | 乘法 | <code>a * b</code> | <code>mul(a, b)</code> |- | 矩阵乘法 | <code>a @ b</code> | <code>matmul(a, b)</code> |- | 取反(算术) | <code>- a</code> | <code>neg(a)</code> |- | 取反(逻辑) | <code>not a</code> | <code>not_(a)</code> |- | 正数 | <code>+ a</code> | <code>pos(a)</code> |- | 右移 | <code>a >> b</code> | <code>rshift(a, b)</code> |- | 切片赋值 | <code>seq[i:j] = values</code> | <code>setitem(seq, slice(i, j), values)</code> |- | 切片删除 | <code>del seq[i:j]</code> | <code>delitem(seq, slice(i, j))</code> |- | 切片取值 | <code>seq[i:j]</code> | <code>getitem(seq, slice(i, j))</code> |- | 字符串格式化 | <code>s % obj</code> | <code>mod(s, obj)</code> |- | 减法 | <code>a - b</code> | <code>sub(a, b)</code> |- | 真值测试 | <code>obj</code> | <code>truth(obj)</code> |- | 比较 | <code>a < b</code> | <code>lt(a, b)</code> |- | 比较 | <code>a <= b</code> | <code>le(a, b)</code> |- | 相等 | <code>a == b</code> | <code>eq(a, b)</code> |- | 不等 | <code>a != b</code> | <code>ne(a, b)</code> |- | 比较 | <code>a >= b</code> | <code>ge(a, b)</code> |- | 比较 | <code>a > b</code> | <code>gt(a, b)</code> |}
返回至“
Python3 operator 模块
”。
上一节:
Python3_pip
下一节:
Python_math_模块