Python数据类型之数字
https://www.runoob.com/python3/ref-math-lgamma.html
数字类型介绍
Python 数字数据类型用于存储数值。
数字数据类型是不允许改变的,这就意味着如果改变数字数据类型的值,将重新分配内存空间。
Python 支持三种不同的数值类型:
- 整型(int) - 通常被称为是整型或整数,是正或负整数,不带小数点。Python3 整型是没有限制大小的,可以当作 Long 类型使用,所以 Python3 没有 Python2 的 Long 类型。布尔(bool)是整型的子类型。
- 浮点型(float) - 浮点型由整数部分与小数部分组成,浮点型也可以使用科学计数法表示(2.5e2 = 2.5 x 102 = 250)
- 复数( (complex)) - 复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型。
数字类型介绍
1、整数类型
与数学中整数概念一致,共有4种进制表示:十进制,二进制,八进制和十六进制。默认情况,整数采用十进制,其它进制需要增加相应的引导符号。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jZcOnz5G-1655443628208)(images/42166d224f4a20a4c077898037db152a730ed09a.jpeg)]
十六进制和八进制表示整数
n = 0xA
print(n) # 10
m = 0o16
print(m) # 14
2、布尔型
布尔型(bool),关键字True和**False,**分别表示真和假,他们的值是1和0,还可和数字相加。
print(True + True) # 2
print(23 * True) # 23
print(31 + True - False) # 32
3、浮点数类型
浮点数类型表示有小数点的数值,浮点数有两种表示方法:小数表示和科学计数法表示。整数具有无限的精度。
number = 1.23
number2 = 3.3333
4、复数类型
表示数学中的复数,复数可以看作二元有序实数对(a, b),表示a + bj,其中,a是实数部分,b为虚数部分。在Python语言中,复数的虚数部分通过后缀 ‘J’ 或 ‘j’ 来表示。复数包含实部和虚部,分别以一个浮点数表示。
print(2 + 2j)
支持的运算
1. 运算符进行计算
# 加
print(32 + 23) # 55
# 减
print(32 - 23) # 9
# 除
print(32 / 5) # 6.4
# 整除(向下取整)
print(32 // 5) # 6
# 整除(向下取整)
print(34 // 5) # 6
# 取余
print(32 % 5) # 2
# 乘
print(32 * 3) # 96
2. 内置函数计算
# abs() 绝对值
print(abs(-22)) # 22
# int() 转换整数,只可转整数类型的数字,对于int以后,不是数字的,会报错
print(int('3')) # 3
# 浮点数
print(float(3)) 3.0
# divmod(a,b)以元组的方式返回a//b(取整)以及a%b的余数
print(divmod(32, 3)) # (10, 2)
# pow() 返回 xy(x 的 y 次方) 的值。
print(pow(2, 3)) # 8
# round(x, y)对x进行四舍五入,保留给定(y)的位数
print(round(3.1415916, 2)) # 3.14
3. 类型转换
print(bool(32)) # True
print(bool(1)) # True
print(bool(0)) # False
print(bool(-1)) # True
对于非0的数字,bool方法转换完都为True, 0转换以后为False.
4. math里面的数学方法
math是python内置的数学运算的包,内置了许多数学计算的函数。math库不支持复数类型,仅支持整数和浮点数运算。
math库一共提供了:
- 4个数字常数
- 44个函数,分为4类
- 16个数值表示函数
- 8个幂对数函数
- 16个三角对数函数
- 4个高等特殊函数
math库的4个常数
# 圆周率 3.141592653589753
print(math.pi)
# 自然对数 2.718281828459045
print(math.e)
# 正无穷大
print(math.inf)
# 非浮点数标记 NAN (not a number)
print(math.nan)
math库的数值表示函数
函数 | 描述 | 数学表示 |
---|---|---|
math.fabs(x) | 返回x的绝对值 | |x| |
math.fmod(x, y) | 返回x与y的模(余数) | x%y |
math.fsum([a, b, c, d, …]) | 浮点数精确求和 | a + b + c + d + … |
math.ceil(x) | 对x进行向上取整 | |
math.floor(x) | 对x进行向下取整 | |
math.factorial(x) | 返回x的阶乘,如果x是小数或者0,返回ValueError | x! |
math.gcd(x, y) | 返回x, y的最大公约数 | |
math.frexp(x) | 表示x = m * 2^n ,表示的是x等于m乘以2的n次方,所以会返回(m,n),当x = 0时,返回(0.0,0) | |
math.ldexp(x, n) | 返回x * 2^n的运算值,是math.frepx(x)函数的反运算 | |
math.modf(x) | 以元组的形式返回x的小数和整数部分 | |
math.trunc(x) | 返回x的整数部分 | |
math.copysign(x, y) | 用数值y的正负号替换数值x的正负号 | |
math.isclose(x, y) | 比较x与y的相似性,返回True或者False | |
math.isfinite(x) | 返回一个布尔值,如果 x 既不是无穷大也不是 NaN,则返回 True ,否则返回 False 。 | |
math.isinf(x) | 返回一个布尔值,如果 x 是正或负无穷大,则返回 True ,否则返回 False 。 | |
math.isnan(x) | 返回一个布尔值,如果数字是 NaN(不是数字),则返回 True ,否则返回 False 。 |
import math
print(math.fmod(9, 2)) # 1.0
print(math.fsum([3, 4, 5])) # 12.0
print(math.ceil(4.3)) # 5
print(math.floor(4.8)) # 4
print(math.factorial(3)) # 6
print(math.gcd(3, 2)) # 1
print(math.frexp(4)) # (0.5, 3)
print(math.ldexp(6, 4)) # 96.0
print(math.modf(5.22)) # (0.21999999999999975, 5.0)
print(math.trunc(5.2)) # 5
print(math.copysign(8, -2)) # -8.0
print(math.isclose(4, 4)) # True
print(math.isfinite(1.3)) # True
print(math.isnan(2)) # False
print(math.isinf(5)) # False
math库的幂对数函数
函数 | 说明 |
---|---|
math.pow(x, y) | 返回x的y次幂 |
math.exp(x) | 一个浮点值,返回 e 的 x 次幂(次方)。e = 2.718281828459 |
math.expm1(x) | 一个浮点值,返回 e 的 x 次幂(次方)减 1。 |
math.sqrt(x) | 返回x的平方根 |
math.log(x, [base]) | 返回一个整数浮点数 float,表示一个数字的自然对数。base可选,默认为e |
math.log1p(x) | 返回x + 1的自然对数值 |
math.log2(x) | 返回x的2对数值 |
math.log10(x) | 返回x的10对数值 |
print(math.pow(3, 3)) # 27.0
print(math.exp(4)) # 54.598150033144236
print(math.expm1(4)) # 53.598150033144236
print(math.sqrt(9)) # 3.0
print(math.log(8, 2)) # 3.0
print(math.log1p(11)) # 2.4849066497880004
print(math.log2(8)) # 3.0
print(math.log10(100)) # 2.0
math库三角双曲函数
函数 | 说明 |
---|---|
math.sin(x) | 返回一个浮点数,表示 x 的正弦值,介于 -1 到 1 之间。 |
math.sinh(x) | 返回一个浮点数,表示一个数字的双曲正弦值。 |
math.asin(x) | 返回 x 的反正弦值,结果范围在 -pi/2 到 pi/2 之间。x取值范围是-1到1之间。 |
math.asinh(x) | 返回 x 的反双曲正弦值。 |
math.cos(x) | 返回一个浮点数,表示 x 的余弦值,介于 -1 到 1 之间。 |
math.acos(x) | 返回 x 的反余弦,结果范围在 0 到 pi 之间。x取值-1 到 1 之间。 |
math.acosh(x) | 返回 x 的反双曲余弦值。 |
math.cosh(x) | 返回 x 的双曲余弦值,相当于 (exp(number) + exp(-number)) / 2。 |
math.tan(x) | 返回 x 弧度的正切值。 |
math.tanh(x) | 返回 x 的双曲正切值。 |
math.atan(x) | 返回 x 的反正切值,以弧度为单位,结果范围在 -pi/2 到 pi/2 之间。 |
math.atanh(x) | 返回 x 的反双曲正切值。x取值-0.99 到 0.99 之间。 |
math.hypot(x, y) | 输入出直角三角形的斜边 |
math.degrees(x) | 将角度 x 从弧度转换为度数。1 弧度等于 57.2957795 度。 |
math.radians(x) | 将角度 x 从度数转换为弧度。 |
print(math.sin(9)) # 0.4121184852417566
print(math.sinh(0.00)) # 0.0
print(math.sinh(-23.45)) # -7641446994.979367
print(math.sinh(23)) # 4872401723.124452
print(math.sinh(1.00)) # 1.1752011936438014
print(math.sinh(math.pi)) # 11.548739357257746
print(math.acos(-1)) # 3.141592653589793
print(math.asin(1)) # 1.5707963267948966
print(math.asinh(0.4)) # 2.8934439858858716
print(math.acosh(9)) # 2.8872709503576206
print(math.tan(90)) # -1.995200412208242
print(math.tanh(90)) # 1.0
print(math.atan(90)) # 1.5596856728972892
print(math.atanh(0.8)) # 1.0986122886681098
print(math.hypot(4, 3)) # 5.0
print(math.sqrt(4*4 + 3*3))
print(math.degrees(9)) # 515.662015617741
print(math.radians(515.662015617741)) # 9.0000000002
math库的特殊函数
函数 | 说明 |
---|---|
math.erf(x) | 返回x的误差函数 |
math.erfc(x) | 返回 x 的互补误差函数。接受 - inf 和 + inf 之间的值,并返回 - 1 到 + 1 之间的值。 |
math.gamma(x) | 返回 x 处的伽玛函数(Gamma 函数)。伽玛函数,也叫欧拉第二积分,是阶乘函数在实数与复数上扩展的一类函数。 |
math.lgamma(x) | 返回一个数字的自然对数伽玛值。 |
print(math.erf(1.28)) # 0.9297341930135783
print(math.erf(-1.28)) # -0.9297341930135783
# 输出不同数字的互补误差函数
print(math.erfc(0.67)) # 0.3433722976996949
print(math.erfc(1.34)) # 0.05808628474163466
# 计算不同数的自然对数伽玛值
print(math.lgamma(7)) # 6.579251212010102
print(math.lgamma(-4.2)) # -1.8075166614192908
# 计算不同数的伽玛函数
print(math.gamma(-0.1)) # -10.686287021193193
print(math.gamma(8)) # 5040.0