Python学习之数字类型及数学函数
一、数字类型(Numeric Types)
Python中有几种主要的数字类型,包括整数(int)、浮点数(float)、复数(complex),三大类型,其中int类型又包括一个子类bool(布尔值)。那么对于这三种类型,介绍大概如下:
- 整数:不包含小数点的数字,用于表示整数值。例如
1
、-5
、1000
等。 - 浮点数:包含小数点的数字,用于表示带有小数点的数字。例如
3.14
、-0.65
、1e6
等。 - 复数:由实数部分和虚数部分组成的数字,虚数部分以 "j" 或 "J" 结尾。例如
1+2j
、3-4j
等。
来看看它们的用法和示例代码:
# 判断数字类型 print(type(1)) # <class 'int'> print(type(3.14)) # <class 'float'> print(type(1+2j)) # <class 'complex'> # 数字类型转换 a = 10 b = 3.14 c = 1+2j print(int(b)) # 3 print(float(a)) # 10.0 print(complex(a)) # (10+0j)
输出结果:
<class 'int'> <class 'float'> <class 'complex'> <class 'bool'> 3 10.0 (10+0j)
这里可以看到,type()函数,用于判断数字的类型。下面我们使用issubclass() 函数来检查 bool 是否是 int 的子类。
# 使用 issubclass() 函数来检查 bool 是否是 int 的子类 print(issubclass(bool, int)) # 输出:True
二、数字运算符的应用
算术运算符用于执行数学运算,逻辑就是按照数学逻辑处理,包括加减乘除、幂运算、取余数等。下面这张列表列出了一些常用的算术运算。
运算符 | 含义 | 示例 |
---|---|---|
+ | 加法 | 1 + 2 = 3 |
- | 减法 | 5 - 3 = 2 |
* | 乘法 | 4 * 3 = 12 |
/ | 浮点除 | 10 / 3 = 3.3333333333333335 |
// | 整除 | 10 // 3 = 3 |
% | 取余数 | 10 % 3 = 1 |
** | 幂运算 | 2 ** 3 = 8 |
示例代码:
a = 10 b = 3 # 加法 print(a + b) # 输出:13 # 减法 print(a - b) # 输出:7 # 乘法 print(a * b) # 输出:30 # 除法 print(a / b) # 输出:3.3333333333333335 # 整除 print(a // b) # 输出:3 # 取模 print(a % b) # 输出:1 # 指数 print(a ** b) # 输出:1000
三、数学函数
这里我们主要讲两个数学函数,一个是Python标准库中的数学函数,另一个是math模块中的数学函数。
下表列出了 Python 标准库中常用的数学函数对比math模块的数学函数,包括函数说明和示例代码。
标准库的函数名 | 说明 | math模块的函数 |
acos(x) | 返回x的反余弦值(弧度) | math.acos(0.5) |
asin(x) | 返回x的反正弦值(弧度) | math.asin(0.5) |
atan(x) | 返回x的反正切值(弧度) | math.atan(0.5) |
atan2(y, x) | 返回以y/x为参数的反正切值(弧度) | math.atan2(1, 1) |
ceil(x) | 返回x向上取整的整数 | math.ceil(3.14) |
cos(x) | 返回x的余弦值 | math.cos(math.pi / 2) |
cosh(x) | 返回x的双曲余弦值 | math.cosh(1) |
degrees(x) | 将弧度转换为角度 | math.degrees(math.pi) |
exp(x) | 返回e的x次方 | math.exp(1) |
fabs(x) | 返回x的绝对值 | math.fabs(-1) |
floor(x) | 返回x向下取整的整数 | math.floor(3.14) |
fmod(x, y) | 返回x除以y的余数 | math.fmod(5, 2) |
frexp(x) | 将x分解为尾数和指数 | math.frexp(1.2345) |
gcd(x, y) | 返回x和y的最大公约数 | math.gcd(12, 18) |
hypot(x, y) | 返回x和y的平方和的平方根 | math.hypot(3, 4) |
isfinite(x) | 判断x是否为有限数 | math.isfinite(1) |
isinf(x) | 判断x是否为无穷大 | math.isinf(float('inf')) |
isnan(x) | 判断x是否为NaN | math.isnan(float('NaN')) |
ldexp(x, i) | 将x乘以2的i次方 | math.ldexp(1.2345, 2) |
log(x) | 返回x的自然对数 | math.log(10) |
log10(x) | 返回x的以10为底的对数 | math.log10(100) |
modf(x) | 将x分解为整数部分和小数部分 | math.modf(1.2345) |
pow(x, y) | 返回x的y次方 | math.pow(2, 3) |
radians(x) | 将角度转换为弧度 | math.radians(90) |
remainder(x, y) | 返回x除以y的余数 | math.remainder(5, 2) |
sin(x) | 返回x的正弦值 | math.sin(math.pi / 2) |
sinh(x) | 返回x的双曲正弦值 | math.sinh(1) |
sqrt(x) | 返回x的平方根 | math.sqrt(4) |
tan(x) | 返回x的正切值 | math.tan(math.pi / 4) |
tanh(x) | 返回x的双曲正切值 | math.tanh(1) |
trunc(x) | 返回x的截断值 | math.trunc(3.14) |
!!!注意:math
模块主要包含围绕平台C数学库函数的简单包装器。特殊情况下的行为在适当情况下遵循C99标准的附录F。
math模块中的函数,返回值均为浮点数,除非另有明确说明。
!!!如果你需要计算复数,请使用 cmath 模块中的同名函数。
以上表格仅列出了部分常用的数学函数,更多函数请参考Python官方文档: https://docs.python.org/zh-cn/3/library/math.html。
以下是几个数学函数的示例代码:
🐡 a. `log(x)`:返回x的自然对数
import math #🌾:计算10的自然对数 x = 10 result = math.log(x) print(f'log({x}) = {result}') # log(10) = 2.302585092994046 #🌾:计算e的自然对数 x = math.e result = math.log(x) print(f"log({x}) = {result}") # log(2.718281828459045) = 1.0
🐡 b. `radians(x)`:将角度转换为弧度
import math # 🌾:将90度转换为弧度 angle = 90 result = math.radians(angle) print(f"radians({angle}) = {result}") # radians(90) = 1.5707963267948966 # 🌾:将180度转换为弧度 angle = 180 result = math.radians(angle) print(f"radians({angle}) = {result}") # radians(180) = 3.141592653589793
🐡 c. `cos(x)`:返回x的余弦值
import math #🌾:计算π/2的余弦值 x = math.pi / 2 result = math.cos(x) print(f"cos({x}) = {result}") # cos(1.5707963267948966) = 6.123233995736766e-17 #🌾:计算π的余弦值 x = math.pi result = math.cos(x) print(f"cos({x}) = {result}") # cos(3.141592653589793) = -1.0
🐡 d. `sin(x)`:返回x的正弦值
import math #🌾:计算π/2的正弦值 x = math.pi / 2 result = math.sin(x) print(f"sin({x}) = {result}") #sin(1.5707963267948966) = 1.0 #🌾:计算π的正弦值 x = math.pi result = math.sin(x) print(f"sin({x}) = {result}") #sin(3.141592653589793) = 1.2246467991473532e-16
🐡 e. `sqrt(x)`:返回x的平方根
import math #🌾:计算4的平方根 x = 4 result = math.sqrt(x) print(f"sqrt({x}) = {result}") # sqrt(4) = 2.0 #🌾:计算9的平方根 x = 9 result = math.sqrt(x) print(f"sqrt({x}) = {result}") # sqrt(9) = 3.0
🐡 f. `exp(x)`:返回e的x次方
import math #🌾:计算e的1次方 x = 1 result = math.exp(x) print(f"exp({x}) = {result}") # exp(1) = 2.718281828459045 #🌾:计算e的2次方 x = 2 result = math.exp(x) print(f"exp({x}) = {result}") # exp(2) = 7.38905609893065
以上代码演示了如何使用Python math 模块中的几个数学函数,标准库中的也类似,不需要math即可。
四、随机数函数
在许多应用中,我们需要生成随机数。Python的random
模块提供了丰富的随机数函数,用于生成随机的整数胡或则随机的浮点数等。
函数 | 描述 |
random.random() | 返回[0.0, 1.0)范围内的随机浮点数 |
random.randint(a, b) | 返回a,b范围内的随机整数 |
random.uniform(a, b) | 返回a,b范围内的随机浮点数 |
random.choice(seq) | 从序列中随机选择一个元素 |
random.shuffle(seq) | 将序列中的元素随机排序 |
random.sample(population, k) | 从总体中选择k个独立的随机元素 |
示例代码:
#🌾:返回[0.0, 1.0)范围内的随机浮点数 import random num = random.random() print(num) #🌾:返回[a, b]范围内的随机整数 num = random.randint(1, 10) print(num) #🌾:返回[a, b]范围内的随机浮点数 num = random.uniform(1.5, 5.5) print(num) #🌾:从序列中随机选择一个元素 items = [1, 2, 3, 4, 5] chosen = random.choice(items) print(chosen) #🌾:将序列中的元素随机排序 items = [1, 2, 3, 4, 5] random.shuffle(items) print(items) #🌾:从总体中选择k个独立的随机元素 items = [1, 2, 3, 4, 5] chosen = random.sample(items, 3) print(chosen)
五、数学常量
Python中一些数学常量,也是数字运算中常用的,在Python中math模块中定义的常用数学常量。通过导入math模块,可以使用这些常量进行数学运算和计算。下面列出一些,供大家参考:
常数 | 描述 |
---|---|
math.pi | 圆周率(π) |
math.e | 自然对数的底(e) |
math.tau | 2π(双倍圆周率) |
math.inf | 无穷大(正无穷) |
math.nan | 非数字(NaN,Not a Number) |
示例代码:
import math #🌾:数学常量示例 print("圆周率 (π) =", math.pi) print("自然对数的底 (e) =", math.e) print("双倍圆周率 (τ) =", math.tau) print("无穷大 =", math.inf) print("非数字 =", math.nan)