GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.runoob.com — Python3 operator 模块 | 菜鸟教程
Python 3 教程
Python3 教程
Python3 简介
Python3 环境搭建
Python3 Pycharm
Python3 VScode
Python3 基础语法
Python3 基本数据类型
Python3 数据类型转换
Python3 解释器
Python3 注释
Python3 运算符
Python3 数字(Number)
Python3 字符串
Python3 列表
Python3 元组
Python3 字典
Python3 集合
Python3 条件控制
Python3 循环语句
Python3 编程第一步
Python3 推导式
Python3 迭代器与生成器
Python3 with
Python3 函数
Python3 lambda
Python3 装饰器
Python3 数据结构
Python3 模块
Python __name__
Python3 输入和输出
Python3 File
Python3 OS
Python3 错误和异常
Python3 面向对象
Python3 命名空间/作用域
Python 虚拟环境的创建
Python 类型注解
Python3 标准库概览
Python3 实例
Python 测验
Python3 高级教程Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQtPython3 operator 模块Python2.x 版本中,使用 cmp() 函数来比较两个列表、数字或字符串等的大小关系。 Python 3.X 的版本中已经没有 cmp() 函数,如果你需要实现比较功能,需要引入 operator 模块,适合任何对象,包含的方法有: operator 模块包含的方法
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) operator.lt(a, b) 与 a < 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 相同。 实例
# 导入 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 = "Runoob" 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))) 以上代码输出结果为: 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: Runoob 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 比较两个列表: 实例
# 导入 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)) 以上代码输出结果为: operator.eq(a,b): False operator.eq(c,b): True 运算符函数operator 模块提供了一套与 Python 的内置运算符对应的高效率函数。例如,operator.add(x, y) 与表达式 x+y 相同。 函数包含的种类有:对象的比较运算、逻辑运算、数学运算以及序列运算。 对象比较函数适用于所有的对象,函数名根据它们对应的比较运算符命名。 许多函数名与特殊方法名相同,只是没有双下划线。为了向后兼容性,也保留了许多包含双下划线的函数,为了表述清楚,建议使用没有双下划线的函数。 实例
# 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)) 以上代码输出结果为: add() 运算结果 :7 sub() 运算结果 :1 mul() 运算结果 :12
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |