文章目录
- 1. [常用python标准库](https://docs.python.org/zh-cn/3/library/index.html)
- 2. math
- 2.1 math模块常用函数
- 2.2 数论与表示函数
- 2.2.1 `math.floor(x)`: x向下取整。
- 2.2.2 `math.ceil(x)`: x向上取整。
- 2.2.3 `math.fabs(x)`: x的绝对值。
- 2.2.4 `math.factorial(x)`: x 的阶乘。
- 2.2.5 `math.prod(iterable, *, start=1)`: iterable中元素的乘积, start表示乘积初始值。
- 2.2.6 `math.isinf(x)`: 如果 x 是正或负无穷大,则返回 True.
- 2.2.7 `math.isnan(x)`: 如果 x 是 NaN(不是数字),则返回 True.
- 2.2.8 `math.isfinite(x)`: 如果 x 既不是无穷大也不是NaN,则返回 True.
- 2.3 常量
- 2.4 幂函数与对数函数
- 2.4.1 math.exp(x): e x e^x ex
- 2.4.2 math.pow(x, y): x y x^y xy
- 2.4.3 math.log(x): l o g e ( x ) log_e(x) loge(x)
- 2.4.4 math.log10(x): l o g 10 ( x ) log_{10}(x) log10(x)
- 2.4.5 math.log2(x): l o g 2 ( x ) log_2(x) log2(x)
- 2.4.6 math.sqrt(x): x sqrt x x
- 2.5 三角函数
- 2.5.1 math.cos(x): c o s ( x ) cos(x) cos(x) x是弧度
- 2.5.2 math.sin(x): s i n ( x ) sin(x) sin(x) x是弧度
- 2.5.3 math.tan(x): t a n ( x ) tan(x) tan(x) x是弧度
- 2.5.4 math.acos(x): a r c c o s ( x ) arccos(x) arccos(x)
- 2.5.5 math.asin(x): a r c s i n ( x ) arcsin(x) arcsin(x)
- 2.5.6 math.atan(x): a r c t a n ( x ) arctan(x) arctan(x)
- 2.5.7 math.dist(p, q): 返回 p 与 q 两点之间的欧式距离
- 2.6 角度转换
- 2.6.1 math.degrees(x): 将角度 x 从弧度转换为度数
- 2.6.2 math.radians(x): 将角度 x 从度数转换为弧度
1. 常用python标准库
- 数字和数学模块:numpy,math,decimal,random,statistic;
- 数据类型:types,collections,datatime,copy,pprint;
- 文本处理:string,re;
- 文件和目录访问:pathlib,os.path,stat,filecmp,tempfile,glob,shutil;
- 数据压缩和存档:zlib,gzip,bz2,zipfile,tarfile;
- 文件格式:csv,configparser;
- 通用操作:os,io,time,argparse,logging;
- 并发执行:threading,multiprocessing;
- 其他:json,sys,contextlib,traceback;
2. math
2.1 math模块常用函数
- 数论与表示函数:
floor/ceil/fabs/factorial/prod/isinf/isnan/isfinite - 常量:
nan/inf/e/pi - 幂函数与对数函数:
exp/pow/log/log10/log2/sqrt - 三角函数:
cos/sin/tan/acos/asin/atan/dist - 角度转换:
degrees/radians
2.2 数论与表示函数
2.2.1 math.floor(x): x向下取整。
n = 3.23
print(math.floor(n))
3
2.2.2 math.ceil(x): x向上取整。
n = 3.23
print(math.ceil(n))
4
2.2.3 math.fabs(x): x的绝对值。
n = -2
print(math.fabs(n))
2.0
2.2.4 math.factorial(x): x 的阶乘。
n = 3
print(math.factorial(n))
6
2.2.5 math.prod(iterable, *, start=1): iterable中元素的乘积, start表示乘积初始值。
n = [2, 3, 5]
print(math.prod(n))
n = [2, 3, 5]
print(math.prod(n, start=2))
30
60
2.2.6 math.isinf(x): 如果 x 是正或负无穷大,则返回 True.
n = math.inf
print(math.isinf(n))
n = 2
print(math.isinf(n))
True
False
2.2.7 math.isnan(x): 如果 x 是 NaN(不是数字),则返回 True.
n = math.nan
print(math.isnan(n))
n = 2
print(math.isnan(n))
True
False
2.2.8 math.isfinite(x): 如果 x 既不是无穷大也不是NaN,则返回 True.
n = math.nan
print(math.isfinite(n))
n = math.inf
print(math.isfinite(n))
n = 2
print(math.isfinite(n))
False
False
True
2.3 常量
print(math.nan)
print(math.inf)
print(math.e)
print(math.pi)
nan
inf
2.718281828459045
3.141592653589793
2.4 幂函数与对数函数
2.4.1 math.exp(x): e x e^x ex
x = 2
print(math.exp(x))
7.38905609893065
2.4.2 math.pow(x, y): x y x^y xy
x = math.e
y = 2
print(pow(x, y))
7.3890560989306495
2.4.3 math.log(x): l o g e ( x ) log_e(x) loge(x)
x = math.e
print(math.log(x))
1.0
2.4.4 math.log10(x): l o g 10 ( x ) log_{10}(x) log10(x)
x = 100
print(math.log10(x))
2.0
2.4.5 math.log2(x): l o g 2 ( x ) log_2(x) log2(x)
x = 4
print(math.log2(x))
2.0
2.4.6 math.sqrt(x): x sqrt x x
x = 9
print(math.sqrt(x))
3.0
2.5 三角函数
2.5.1 math.cos(x): c o s ( x ) cos(x) cos(x) x是弧度
import matplotlib.pyplot as plt
import numpy as np
ys = []
xs = np.linspace(-2 * np.pi, 2 * np.pi, 100)
for i in xs:
ys.append(math.cos(i))
plt.plot(xs, ys)
plt.show()
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mk2fXyVE-1644747828021)(output_38_0.png)]](https://file2.kaopuke.com:8081/files_image/2023060421/4341964eb8124b7896b1a26b47845f91.png)
2.5.2 math.sin(x): s i n ( x ) sin(x) sin(x) x是弧度
import matplotlib.pyplot as plt
import numpy as np
ys = []
xs = np.linspace(-2 * np.pi, 2 * np.pi, 100)
for i in xs:
ys.append(math.sin(i))
plt.plot(xs, ys)
plt.show()
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tDjznof0-1644747828024)(output_40_0.png)]](https://file2.kaopuke.com:8081/files_image/2023060421/bba1088429f34e40a813eccb78302e08.png)
2.5.3 math.tan(x): t a n ( x ) tan(x) tan(x) x是弧度
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
xs = np.linspace(-2 * np.pi, 2 * np.pi, 200)
ys = []
for i in xs:
ys.append(math.tan(i))
# plt.plot(x, f1, label=r'$y=sin(x)$')
# plt.plot(x, f2, label=r'$y=cos(x)$')
plt.plot(xs, ys, label=r'$y=tan(x)$')
# plt.plot(x, f4, label=r'$y=cot(x)$')
# plt.plot(x, f5, label=r'$y=sec(x)$', )
# plt.plot(x, f6, label=r'$y=csc(x)$', )
# 移动坐标轴及边框设置:https://www.jb51.net/article/172275.htm
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
plt.xlim(x.min() * 1.1, x.max() * 1.1) # limit x range
plt.ylim(-4, 4) # limit y range
plt.xticks([-2 * np.pi, -3 * np.pi / 2, -np.pi, -np.pi / 2, 0, np.pi / 2, np.pi, 3 * np.pi / 2, 2 * np.pi],
[r'$-2pi$', r'$-3pi/2$', r'$-pi$', r'$-pi/2$', r'$0$', r'$pi/2$', r'$pi$', r'$3pi/2$', r'$2pi$'])
plt.legend(loc='best')
plt.show()
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LiJdL8u5-1644747828025)(output_42_0.png)]](https://file2.kaopuke.com:8081/files_image/2023060421/2ca828b8a4dd4e8a978911c60d018597.png)
2.5.4 math.acos(x): a r c c o s ( x ) arccos(x) arccos(x)
import matplotlib.pyplot as plt
import numpy as np
ys = []
xs = np.linspace(-1, 1, 100)
for i in xs:
ys.append(math.acos(i))
plt.plot(xs, ys)
plt.show()
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GVEVuyuJ-1644747828026)(output_44_0.png)]](https://file2.kaopuke.com:8081/files_image/2023060421/3ebd1d49c3e74a3d8fd5125c6e48ae22.png)
2.5.5 math.asin(x): a r c s i n ( x ) arcsin(x) arcsin(x)
import matplotlib.pyplot as plt
import numpy as np
ys = []
xs = np.linspace(-1, 1, 100)
for i in xs:
ys.append(math.asin(i))
plt.plot(xs, ys)
plt.show()
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NSsFdJNa-1644747828027)(output_46_0.png)]](https://file2.kaopuke.com:8081/files_image/2023060421/f03a498b7f4e46f18c1cd5e5ec1474ab.png)
2.5.6 math.atan(x): a r c t a n ( x ) arctan(x) arctan(x)
import matplotlib.pyplot as plt
import numpy as np
ys = []
xs = np.linspace(-10, 10, 100)
for i in xs:
ys.append(math.atan(i))
plt.plot(xs, ys)
plt.show()
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MFNGlE9k-1644747828027)(output_48_0.png)]](https://file2.kaopuke.com:8081/files_image/2023060421/3ca85a003988403f8f7c0ce2f6d899d7.png)
2.5.7 math.dist(p, q): 返回 p 与 q 两点之间的欧式距离
p = [3]
q = [1]
print (math.dist(p, q))
p = [0, 0]
q = [1, 1]
print (math.dist(p, q))
2.0
1.4142135623730951
2.6 角度转换
2.6.1 math.degrees(x): 将角度 x 从弧度转换为度数
x = math.pi
print(math.degrees(x))
x = math.pi / 2
print(math.degrees(x))
180.0
90.0
2.6.2 math.radians(x): 将角度 x 从度数转换为弧度
x = 180
print(math.radians(x))
x = 90
print(math.radians(x))
3.141592653589793
1.5707963267948966
最后
以上就是烂漫棉花糖最近收集整理的关于python标准库学习——math模块1. 常用python标准库2. math的全部内容,更多相关python标准库学习——math模块1.内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复