- Python教程
- Python 简介
- Python3 下载安装
- python基础语法
- Python基本数据类型
- Python数据类型转换
- Python解释器
- Python 注释
- Python运算符
- Python数字(Number)
- Python字符串
- Python列表
- Python元组
- Python3 字典
- Python集合
- Python条件控制
- Python循环语句
- Python编程第一步
- Python 推导式
- Python3 迭代器与生成器
- Python函数
- Python lambda(匿名函数)
- Python 装饰器
- Python数据结构
- Python3 模块
- Python __name__ 与 __main__
- Python输入和输出
- Python3 File(文件) 方法
- Python3 OS 文件/目录方法
- Python3 错误和异常
- Python3 面向对象
- Python3 命名空间和作用域
- Python3 标准库概览
- -----高级教程----------
- Python3 正则表达式
- Python CGI编程
- Python MySQL - mysql-connector 驱动
- Python3 MySQL 数据库连接 - PyMySQL 驱动
- Python3 网络编程
- Python3 SMTP发送邮件
- Python3 多线程
- Python3 XML 解析
- Python3 JSON 数据解析
- Python3 日期和时间
- Python MongoDB
- **Python Mongodb 插入文档
- **Python Mongodb 查询文档
- **Python Mongodb 修改文档
- **Python Mongodb 排序
- **Python Mongodb 删除数据
- Python urllib
- Python uWSGI 安装配置
- Python3 pip
- Anaconda 教程
- Python3 operator 模块
- Python math 模块
- Python requests 模块
- Python random 模块
- Python AI 绘画
- Python statistics 模块
- Python hashlib 模块
- Python 量化
- Python pyecharts 模块
- Python selenium 库
- Python 爬虫 - BeautifulSoup
- Python Scrapy 库
- Python Markdown 生成 HTML
- Python sys 模块
- Python Pickle 模块
- Python subprocess 模块
- Python queue 模块
- Python StringIO 模块
- Python logging 模块
- Python datetime 模块
- Python re 模块
- Python csv 模块
- Python threading 模块
- Python asyncio 模块
- Python PyQt
- **Python PyQt 常用组件
- **Python PyQt 布局管理
- **Python PyQt 信号与槽机制
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)))
以上代码输出结果为:
比较两个列表:
实例
# 导入 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 模块提供了一套与 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))
以上代码输出结果为:
运算 | 语法 | 函数 |
---|---|---|
加法 |
|
|
字符串拼接 |
|
|
包含测试 |
|
|
除法 |
|
|
除法 |
|
|
按位与 |
|
|
按位异或 |
|
|
按位取反 |
|
|
按位或 |
|
|
取幂 |
|
|
标识 |
|
|
标识 |
|
|
索引赋值 |
|
|
索引删除 |
|
|
索引取值 |
|
|
左移 |
|
|
取模 |
|
|
乘法 |
|
|
矩阵乘法 |
|
|
取反(算术) |
|
|
取反(逻辑) |
|
|
正数 |
|
|
右移 |
|
|
切片赋值 |
|
|
切片删除 |
|
|
切片取值 |
|
|
字符串格式化 |
|
|
减法 |
|
|
真值测试 |
|
|
比较 |
|
|
比较 |
|
|
相等 |
|
|
不等 |
|
|
比较 |
|
|
比较 |
|
|