自学内容网 自学内容网

「Python系列」Python operator模块、math模块

一、Python operator模块

Python的operator模块提供了一系列内置的操作符函数,这些函数对应于Python语言中的内建操作符。使用operator模块可以使代码更加清晰和易读,同时也能提高性能,因为它通常比使用Python内建操作符更快。

下面是一些operator模块中常用的函数:

1. 算术操作符

  • operator.add(a, b): 返回 a + b
  • operator.sub(a, b): 返回 a - b
  • operator.mul(a, b): 返回 a * b
  • operator.truediv(a, b): 返回 a / b(浮点数除法)。
  • operator.floordiv(a, b): 返回 a // b(整数除法)。
  • operator.mod(a, b): 返回 a % b(取余)。
  • operator.pow(a, b): 返回 a ** b(幂运算)。
  • operator.neg(a): 返回 -a(取负)。
  • operator.pos(a): 返回 +a(取正)。
  • operator.abs(a): 返回 abs(a)(绝对值)。

2. 比较操作符

  • operator.eq(a, b): 如果 a == b 返回 True
  • operator.ne(a, b): 如果 a != b 返回 True
  • operator.lt(a, b): 如果 a < b 返回 True
  • operator.le(a, b): 如果 a <= b 返回 True
  • operator.gt(a, b): 如果 a > b 返回 True
  • operator.ge(a, b): 如果 a >= b 返回 True

3. 逻辑操作符

  • operator.and_(a, b): 返回 a and b(逻辑与)。
  • operator.or_(a, b): 返回 a or b(逻辑或)。
  • operator.not_(a): 返回 not a(逻辑非)。

4. 序列操作符

  • operator.concat(a, b): 返回 a + b(字符串或序列拼接)。
  • operator.contains(a, b): 如果 ba 中,返回 True(例如,b 是否是 a 的子字符串或子序列)。
  • operator.countOf(a, b): 返回 ba 中出现的次数(仅对序列有效)。
  • operator.indexOf(a, b, [start, [stop]]): 返回 ba 中首次出现的索引,如果 b 不在 a 中则返回 -1(仅对序列有效)。

5. 映射和集合操作符

  • operator.itemgetter(item): 返回一个可调用对象,该对象从其操作数中获取指定的项目。
  • operator.attrgetter(attr): 返回一个可调用对象,该对象从其操作数中获取指定的属性。
  • operator.methodcaller(method, *args, **kwargs): 返回一个可调用对象,该对象调用其操作数的指定方法。

6. 其他操作符

  • operator.truth(a): 如果 a 为真(即非零或非空),返回 a
  • operator.false(a): 如果 a 为假(即零或空),返回 a
  • operator.index(a): 返回 a 的“索引”值,这通常是 a 本身,但对于字符串,它返回字符串的哈希值。
  • operator.getitem(obj, index): 相当于 obj[index],用于获取对象(通常是映射或序列)中指定索引或键的值。
  • operator.setitem(obj, index, value): 相当于 obj[index] = value,用于设置对象(通常是映射或序列)中指定索引或键的值。
  • operator.delitem(obj, index): 相当于 del obj[index],用于删除对象(通常是映射或序列)中指定索引或键的项。
  • operator.has_key(obj, key): 检查对象(通常是映射)中是否包含指定的键,相当于 key in obj。注意:这个函数在Python 3中已被移除,应该使用in关键字替代。

这些函数可以作为functools.reduce()函数或其他需要二元操作符的函数的参数,以在映射上进行操作。

二、operator模块案例

1. 案例一

假设我们有一个字典,我们想使用operator.getitem来获取字典中某个键的值:

import operator

# 创建一个字典
my_dict = {'a': 1, 'b': 2, 'c': 3}

# 使用 operator.getitem 获取键 'b' 的值
value = operator.getitem(my_dict, 'b')
print(value)  # 输出: 2

假设我们还有一个列表的列表(即二维列表),我们想使用operator.getitem来获取每个子列表的第一个元素:

import operator

# 创建一个二维列表
two_dim_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# 使用 operator.getitem 和 map 函数获取每个子列表的第一个元素
first_elements = list(map(operator.getitem, two_dim_list, [0]*len(two_dim_list)))
print(first_elements)  # 输出: [1, 4, 7]

在这个例子中,map()函数接受三个参数:operator.getitem函数,二维列表two_dim_list,以及一个包含多个0的列表(与two_dim_list的长度相同)。map()函数将operator.getitem应用到two_dim_list的每个子列表上,并使用0作为索引来获取每个子列表的第一个元素。

2. 示例二

import operator

# 使用算术操作符
result = operator.add(3, 4)
print(result)  # 输出: 7

# 使用比较操作符
is_equal = operator.eq(result, 7)
print(is_equal)  # 输出: True

# 使用序列操作符
str1 = "Hello"
str2 = "World"
combined = operator.concat(str1, " ")
combined = operator.concat(combined, str2)
print(combined)  # 输出: Hello World

三、Python math模块

Python的math模块提供了一系列数学函数和常量,用于进行浮点数数学运算。这个模块中的所有函数都接受浮点数作为参数,并返回浮点数结果。下面是一些math模块中常用的函数和常量:

1. 常量

  • math.pi: 圆的周长与直径之比,即π的值。
  • math.e: 自然对数的底数,即e的值。
  • math.tau: 圆的周长与半径之比,即τ的值(在Python 3.6及更高版本中可用)。

2. 三角函数

  • math.sin(x): 返回x的正弦值。
  • math.cos(x): 返回x的余弦值。
  • math.tan(x): 返回x的正切值。
  • math.asin(x): 返回x的反正弦值。
  • math.acos(x): 返回x的反余弦值。
  • math.atan(x): 返回x的反正切值。
  • math.atan2(y, x): 返回给定的两个参数的反正切值(四象限的角度)。

3. 双曲函数

  • math.sinh(x): 返回x的双曲正弦值。
  • math.cosh(x): 返回x的双曲余弦值。
  • math.tanh(x): 返回x的双曲正切值。
  • math.asinh(x): 返回x的反双曲正弦值。
  • math.acosh(x): 返回x的反双曲余弦值。
  • math.atanh(x): 返回x的反双曲正切值。

4. 指数和对数函数

  • math.exp(x): 返回e的x次幂。
  • math.log(x[, base]): 返回x的自然对数(默认以e为底),如果指定了base参数,则返回以该参数为底的对数。
  • math.log10(x): 返回x的以10为底的对数。
  • math.pow(x, y): 返回x的y次幂。
  • math.sqrt(x): 返回x的平方根。

5. 其他函数

  • math.ceil(x): 返回大于或等于x的最小整数(向上取整)。
  • math.floor(x): 返回小于或等于x的最大整数(向下取整)。
  • math.trunc(x): 返回x的整数部分(去除小数部分)。
  • math.fabs(x): 返回x的绝对值。
  • math.factorial(x): 返回x的阶乘。
  • math.fmod(x, y): 返回x除以y的余数。
  • math.gcd(x, y): 返回x和y的最大公约数。
  • math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0): 判断两个浮点数是否接近。
  • math.isfinite(x): 检查x是否是有穷的(即不是无穷大或NaN)。
  • math.isinf(x): 检查x是否是正无穷或负无穷。
  • math.isnan(x): 检查x是否是NaN(不是一个数字)。

6. 示例

一个使用math模块中函数的简单示例:

import math

# 使用三角函数
angle_in_radians = math.pi / 4  # 45度转换为弧度
sine_value = math.sin(angle_in_radians)
print(sine_value)  # 输出: 0.7071067811865476

# 使用指数和对数函数
base = 2
exponent = 3
result = math.pow(base, exponent)
print(result)  # 输出: 8.0

# 使用其他函数
number = 7.9
rounded_down = math.floor(number)
rounded_up = math.ceil(number)
print(rounded_down)  # 输出: 7
print(rounded_up)    # 输出: 8

# 检查数字是否为有限数、无穷大或NaN
finite_number = 10
infinite_number = float('inf')
nan_number = float('nan')

print(math.isfinite(finite_number))  # 输出: True
print(math.isinf(infinite_number))   # 输出: True
print(math.isnan(nan_number))        # 输出: True

四、相关链接

  1. Python下载安装中心
  2. Python官网
  3. Python软件下载
  4. 「Python系列」Python简介及案例
  5. 「Python系列」Python基础语法/数据类型
  6. 「Python系列」Python解释器
  7. 「Python系列」Python运算符
  8. 「Python系列」Python数据结构
  9. 「Python系列」Python元组
  10. 「Python系列」Python集合
  11. 「Python系列」Python列表

原文地址:https://blog.csdn.net/xuaner8786/article/details/136347065

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!