Python3 数字(Number)
整数、浮点、复数的类型转换、运算规则与 math 模块常用函数常量。
Python 数字类型转换
Python has three numeric types: int (arbitrary precision), float (double), complex. Convert with int() / float() / complex(); note int() truncates toward zero instead of rounding.
a = 5
b = 2.5
c = 3 + 4j
print(int(b), float(a), complex(a))
print(int(-2.9), round(2.5), round(3.5))
print(int('42'), float('3.14'))
print(type(int(b)).__name__, c.real, c.imag)2 5.0 (5+0j)
-2 2 4
42 3.14
int 3.0 4.0Python 数字运算
/ is always true division returning a float, // floors (it does not truncate), % takes the sign of the divisor, and ** is exponentiation. Integers never overflow; floats do.
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)
print(-7 // 2)
print(divmod(7, 2))
x = 5
x += 3
print(x)
print(abs(-3), pow(2, 8), pow(2, 8, 100))
print(0.1 + 0.2 == 0.3, round(0.1 + 0.2, 10) == 0.3)3.5
3
1
1024
-4
(3, 1)
8
3 256 56
False True0.1 + 0.2 is not 0.3. Compare with math.isclose(a, b), or use decimal.Decimal when exact decimals matter.数学函数
The math module supplies the usual functions: ceil / floor for rounding, fabs for absolute value (float result), exp / log / log10 / log2, and sqrt. They expect numbers and do no type coercion.
import math
print(math.ceil(4.1), math.floor(4.9))
print(math.fabs(-5), abs(-5))
print(math.exp(1))
print(math.log(math.e), math.log10(100), math.log2(8))
print(math.sqrt(16))
print(math.modf(3.7))5 4
5.0 5
2.718281828459045
1.0 2.0 3.0
4.0
(0.7000000000000002, 3.0)随机数函数
random yields pseudo-random numbers: random() in [0,1), randint(a,b) inclusive integers, choice(seq) one element, uniform(a,b) a float, shuffle(list) in place. seed() makes runs reproducible.
import random
random.seed(42)
print(random.random())
print(random.randint(1, 10))
print(random.choice(['a', 'b', 'c']))
random.seed(42)
print(round(random.uniform(1, 10), 2))
deck = list(range(1, 6))
random.shuffle(deck)
print(deck)
random.seed(7)
print([random.randint(1, 6) for _ in range(5)])0.6394267984578837
1
c
6.75
[2, 4, 5, 3, 1]
[3, 2, 4, 6, 1]random is pseudo-random: fine for simulation and tests, not for cryptography. Use the secrets module for keys and tokens.三角函数
Trigonometric functions work in radians: sin / cos / tan take radians and asin / acos / atan return them. Convert with degrees() / radians(); hypot(x, y) computes the hypotenuse directly.
import math
print(math.sin(0), math.cos(0), math.tan(0))
print(math.sin(math.pi / 2))
print(math.degrees(math.pi), math.radians(180))
print(math.asin(1), math.acos(1))
print(math.hypot(3, 4))0.0 1.0 0.0
1.0
180.0 3.141592653589793
1.5707963267948966 0.0
5.0数学常量
math.pi, math.e and math.tau are the usual constants. math.inf is infinity and math.nan is not-a-number — nan compares False against everything, including itself.
import math
print(math.pi)
print(math.e)
print(math.tau)
print(math.inf, -math.inf)
print(math.isnan(math.nan), math.isfinite(math.inf))
print(math.nan == math.nan, math.isnan(math.nan))3.141592653589793
2.718281828459045
6.283185307179586
inf -inf
True False
False TruePython 数字类型转换
Python 有三种数字类型:int(任意精度整数)、float(双精度浮点)、complex(复数)。用 int() / float() / complex() 相互转换,int() 直接截断小数部分而不是四舍五入。
a = 5
b = 2.5
c = 3 + 4j
print(int(b), float(a), complex(a))
print(int(-2.9), round(2.5), round(3.5))
print(int('42'), float('3.14'))
print(type(int(b)).__name__, c.real, c.imag)2 5.0 (5+0j)
-2 2 4
42 3.14
int 3.0 4.0Python 数字运算
/ 永远是真除法返回浮点,// 是向下取整(不是截断),% 的结果符号跟除数一致,** 是幂运算。整数不会溢出,浮点则有精度上限。
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)
print(-7 // 2)
print(divmod(7, 2))
x = 5
x += 3
print(x)
print(abs(-3), pow(2, 8), pow(2, 8, 100))
print(0.1 + 0.2 == 0.3, round(0.1 + 0.2, 10) == 0.3)3.5
3
1
1024
-4
(3, 1)
8
3 256 56
False True0.1 + 0.2 并不等于 0.3。比较浮点用 math.isclose(a, b),需要精确十进制时用 decimal.Decimal。数学函数
math 模块提供常用数学函数:取整用 ceil / floor,绝对值用 fabs(返回浮点),指数对数用 exp / log / log10 / log2,开方用 sqrt。这些函数只接受数值,不做类型转换。
import math
print(math.ceil(4.1), math.floor(4.9))
print(math.fabs(-5), abs(-5))
print(math.exp(1))
print(math.log(math.e), math.log10(100), math.log2(8))
print(math.sqrt(16))
print(math.modf(3.7))5 4
5.0 5
2.718281828459045
1.0 2.0 3.0
4.0
(0.7000000000000002, 3.0)随机数函数
random 模块生成伪随机数:random() 取 [0,1) 浮点、randint(a,b) 取闭区间整数、choice(seq) 随机取一个元素、uniform(a,b) 取浮点、shuffle(list) 就地打乱。设置 seed() 可复现序列。
import random
random.seed(42)
print(random.random())
print(random.randint(1, 10))
print(random.choice(['a', 'b', 'c']))
random.seed(42)
print(round(random.uniform(1, 10), 2))
deck = list(range(1, 6))
random.shuffle(deck)
print(deck)
random.seed(7)
print([random.randint(1, 6) for _ in range(5)])0.6394267984578837
1
c
6.75
[2, 4, 5, 3, 1]
[3, 2, 4, 6, 1]random 是伪随机,用于模拟和测试足够,不能用于密码学。生成密钥、令牌请用 secrets 模块。三角函数
三角函数以弧度为单位:sin / cos / tan 接收弧度,asin / acos / atan 返回弧度。角度与弧度互转用 degrees() 和 radians(),hypot(x, y) 直接算直角三角形斜边。
import math
print(math.sin(0), math.cos(0), math.tan(0))
print(math.sin(math.pi / 2))
print(math.degrees(math.pi), math.radians(180))
print(math.asin(1), math.acos(1))
print(math.hypot(3, 4))0.0 1.0 0.0
1.0
180.0 3.141592653589793
1.5707963267948966 0.0
5.0数学常量
math.pi 是圆周率、math.e 是自然常数、math.tau 是 2π。math.inf 表示无穷大,math.nan 表示非数值 —— 注意 nan 与任何值比较都是 False,包括它自己。
import math
print(math.pi)
print(math.e)
print(math.tau)
print(math.inf, -math.inf)
print(math.isnan(math.nan), math.isfinite(math.inf))
print(math.nan == math.nan, math.isnan(math.nan))3.141592653589793
2.718281828459045
6.283185307179586
inf -inf
True False
False True