Python3 数字(Number)

整数、浮点、复数的类型转换、运算规则与 math 模块常用函数常量。

Beginner
Key PointsPython 整数无溢出、除法恒为真除法;// 向下取整、% 符号跟随除数;浮点有精度误差,比较用 math.isclose。

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.

Python
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)
Output
2 5.0 (5+0j)
-2 2 4
42 3.14
int 3.0 4.0

Python 数字运算

/ 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.

Python
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)
Output
3.5
3
1
1024
-4
(3, 1)
8
3 256 56
False True
Floats are binary approximations, so 0.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.

Python
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))
Output
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.

Python
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)])
Output
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.

Python
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))
Output
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.

Python
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))
Output
3.141592653589793
2.718281828459045
6.283185307179586
inf -inf
True False
False True

Friends & Partners