Python学习笔记21:进阶篇(十)常见标准库使用之math模块,random模块和statistics模块

前言

本文是根据python官方教程中标准库模块的介绍,自己查询资料并整理,编写代码示例做出的学习笔记。

根据模块知识,一次讲解单个或者多个模块的内容。

教程链接:https://docs.python.org/zh-cn/3/tutorial/index.html

数学

Python中还有一些模块是应用于数学方面的模块。

math

math模块。主要用于处理数学相关的计算需求,包括但不限于基本的算术运算、对数、三角函数、常数定义等

常用函数

  1. math.sqrt(x): 计算非负数x的平方根。
  2. math.pi: 提供π的值,约等于3.14159。
  3. math.sin(x): 计算弧度x的正弦值。
  4. math.cos(x): 计算弧度x的余弦值。
  5. math.tan(x): 计算弧度x的正切值。
  6. math.exp(x): 计算e的x次幂,其中e约等于2.71828。
  7. math.log(x[, base]): 计算x的自然对数(默认)或以base为底的对数。
  8. math.log10(x): 计算x的以10为底的对数。
  9. math.ceil(x): 返回不小于x的最小整数。
  10. math.floor(x): 返回不大于x的最大整数。
  11. math.fabs(x): 计算x的绝对值。
  12. math.factorial(x): 计算x的阶乘(x必须是非负整数)。
  13. math.gcd(a, b): 计算a和b的最大公约数。

示例

import math

# math.sqrt(x): 计算非负数x的平方根
sqrt_example = math.sqrt(16)
print("Square root of 16 is", sqrt_example)
print("===============分隔符==============")

# math.pi: 提供π的值
pi_example = math.pi
print("Value of pi is approximately", pi_example)
print("===============分隔符==============")

# math.sin(x), math.cos(x), math.tan(x): 计算弧度x的三角函数值
angle_degrees = 45
angle_radians = math.radians(angle_degrees)
sin_example = math.sin(angle_radians)
cos_example = math.cos(angle_radians)
tan_example = math.tan(angle_radians)
print(f"Sine of {angle_degrees} degrees is {sin_example}")
print(f"Cosine of {angle_degrees} degrees is {cos_example}")
print(f"Tangent of {angle_degrees} degrees is {tan_example}")
print("===============分隔符==============")

# math.exp(x): 计算e的x次幂
exp_example = math.exp(1)
print("e^1 is", exp_example)
print("===============分隔符==============")

# math.log(x[, base]): 计算x的对数
log_example = math.log(100)  # 自然对数
log_base_10_example = math.log(100, 10)  # 以10为底
print("Natural log of 100 is", log_example)
print("Log base 10 of 100 is", log_base_10_example)
print("===============分隔符==============")

# math.log10(x): 计算x的以10为底的对数
log10_example = math.log10(100)
print("Log base 10 of 100 is", log10_example)
print("===============分隔符==============")

# math.ceil(x), math.floor(x): 向上取整和向下取整
ceil_example = math.ceil(3.7)
floor_example = math.floor(3.7)
print("Ceiling of 3.7 is", ceil_example)
print("Floor of 3.7 is", floor_example)
print("===============分隔符==============")

# math.fabs(x): 计算x的绝对值
fabs_example = math.fabs(-42)
print("Absolute value of -42 is", fabs_example)
print("===============分隔符==============")

# math.factorial(x): 计算x的阶乘
factorial_example = math.factorial(5)
print("Factorial of 5 is", factorial_example)
print("===============分隔符==============")

# math.gcd(a, b): 计算a和b的最大公约数
gcd_example = math.gcd(48, 18)
print("GCD of 48 and 18 is", gcd_example)

在这里插入图片描述

random

random模块提供了生成随机数的功能,适用于各种用途,比如模拟、游戏开发、安全应用中的随机数需求等。它包含了一系列用于生成不同分布的随机数的函数。

常用函数

  1. random.randint(a, b): 生成一个位于a和b之间(含两端)的随机整数。
  2. random.random(): 生成一个位于[0, 1)范围内的随机浮点数。
  3. random.uniform(a, b): 生成一个位于a和b之间(含两端)的随机浮点数。
  4. random.choice(seq): 从序列seq中随机选择一个元素。
  5. random.shuffle(x): 随机地就地打乱列表x的元素顺序。
  6. random.sample(population, k): 从总体序列中无放回地选择k个随机元素。

示例

import random


random_integer = random.randint(1, 100)
print("Random integer between 1 and 100 is", random_integer)
print("===============分隔符==============")
random_float = random.random()
print("Random float between 0 and 1 is", random_float)
print("===============分隔符==============")
random_uniform = random.uniform(1, 10)
print("Random uniform float between 1 and 10 is", random_uniform)
print("===============分隔符==============")
my_list = [1, 2, 3, 4, 5]
random_selection = random.choice(my_list)
print("Randomly selected item from list is", random_selection)
print("===============分隔符==============")
my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
print("List after shuffling is", my_list)
print("===============分隔符==============")
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sample = random.sample(my_list, 3)

在这里插入图片描述

statistics

statistics模块提供了用于计算一系列数值数据的基本统计信息的函数,如平均数、中位数、众数等。它适用于数据分析、数学计算等领域。

常用函数

  1. statistics.mean(data): 计算数据集的算术平均值。
  2. statistics.median(data): 找到数据集的中位数。
  3. statistics.mode(data): 返回数据集的众数,如果有多个众数,则会引发StatisticsError或返回其中一个。
  4. statistics.stdev(data[, xbar]): 计算样本标准差,可选参数xbar用于指定平均值。
  5. statistics.variance(data[, xbar]): 计算样本方差,可选参数xbar用于指定平均值。
  6. statistics.quantiles(data, n): 计算数据集的分位数,n表示分位数的数量或具体的分位数值。

实例

import statistics


data = [1, 2, 3, 4, 5]
mean_value = statistics.mean(data)
print("Mean of data is", mean_value)
print("===============分隔符==============")
data = [1, 2, 3, 4, 5]
median_value = statistics.median(data)
print("Median of data is", median_value)
print("===============分隔符==============")
data = [1, 2, 2, 3, 4, 4, 4, 5]
try:
    mode_value = statistics.mode(data)
    print("Mode of data is", mode_value)
except statistics.StatisticsError as e:
    print("Data has multiple modes or no mode:", e)
print("===============分隔符==============")
data = [10, 12, 23, 23, 16, 23, 21, 16]
stdev_value = statistics.stdev(data)
print("Sample standard deviation of data is", stdev_value)
print("===============分隔符==============")
data = [1.5, 2.5, 2.5, 2.75, 3.25, 4.75]
variance_value = statistics.variance(data)
print("Sample variance of data is", variance_value)
print("===============分隔符==============")
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
quantiles = statistics.quantiles(data, n=4)  # 计算四个分位数
print("Quantiles of data are", quantiles)

在这里插入图片描述

结尾

关于Python中数学的相关模块,按教程中就讲这三个模块。由于都是一些函数调用,也没有特别的知识点要讲。不过也要熟悉常用的数学函数的使用,项目中是很容易用到的。

作业

  1. 使用math模块的常用函数。
  2. 使用random中的常用函数。
  3. 使用statistics中的常用函数,
  • 24
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值