Appearance
Python 数字
Python 中的数字类型是用于表示数值的数据类型,包括整数、浮点数和复数。本章节将详细介绍 Python 中的数字类型及其操作。
数字类型
Python 支持三种不同的数字类型:
- 整数(int):表示没有小数部分的数字,可以是正数、负数或零。
- 浮点数(float):表示带有小数部分的数字。
- 复数(complex):表示具有实部和虚部的复数。
整数(int)
Python 中的整数可以是任意大小,不受机器字长的限制。
示例:
python
# 整数示例
x = 10
print(type(x)) # 输出:<class 'int'>
# 大整数
y = 1000000000000000000000000000000
print(y) # 输出:1000000000000000000000000000000
print(type(y)) # 输出:<class 'int'>
# 负数
z = -5
print(z) # 输出:-5
print(type(z)) # 输出:<class 'int'>
# 零
zero = 0
print(zero) # 输出:0
print(type(zero)) # 输出:<class 'int'>浮点数(float)
浮点数用于表示带有小数部分的数字。
示例:
python
# 浮点数示例
x = 3.14
print(x) # 输出:3.14
print(type(x)) # 输出:<class 'float'>
# 科学计数法表示的浮点数
y = 1.23e-4
print(y) # 输出:0.000123
print(type(y)) # 输出:<class 'float'>
z = 2.5e3
print(z) # 输出:2500.0
print(type(z)) # 输出:<class 'float'>
# 整数转换为浮点数
w = float(10)
print(w) # 输出:10.0
print(type(w)) # 输出:<class 'float'>复数(complex)
复数由实部和虚部组成,虚部以 j 或 J 结尾。
示例:
python
# 复数示例
x = 3 + 4j
print(x) # 输出:(3+4j)
print(type(x)) # 输出:<class 'complex'>
# 访问实部和虚部
print(x.real) # 输出:3.0
print(x.imag) # 输出:4.0
# 复数运算
y = 1 + 2j
z = x + y
print(z) # 输出:(4+6j)
z = x - y
print(z) # 输出:(2+2j)
z = x * y
print(z) # 输出:(-5+10j)
z = x / y
print(z) # 输出:(2+0j)
# 共轭复数
print(x.conjugate()) # 输出:(3-4j)数字运算
Python 支持各种数字运算,包括算术运算、比较运算和逻辑运算。
算术运算
python
# 算术运算示例
x = 10
y = 3
print(x + y) # 加法,输出:13
print(x - y) # 减法,输出:7
print(x * y) # 乘法,输出:30
print(x / y) # 除法,输出:3.3333333333333335
print(x % y) # 取模,输出:1
print(x ** y) # 幂运算,输出:1000
print(x // y) # 整除,输出:3比较运算
python
# 比较运算示例
x = 10
y = 3
print(x == y) # 等于,输出:False
print(x != y) # 不等于,输出:True
print(x > y) # 大于,输出:True
print(x < y) # 小于,输出:False
print(x >= y) # 大于等于,输出:True
print(x <= y) # 小于等于,输出:False内置数学函数
Python 提供了许多内置的数学函数,用于执行各种数学运算。
基本数学函数
| 函数 | 描述 | 示例 |
|---|---|---|
abs(x) | 返回 x 的绝对值 | abs(-5) 返回 5 |
pow(x, y) | 返回 x 的 y 次幂 | pow(2, 3) 返回 8 |
max(x1, x2, ...) | 返回最大值 | max(1, 2, 3) 返回 3 |
min(x1, x2, ...) | 返回最小值 | min(1, 2, 3) 返回 1 |
round(x, n) | 四舍五入,n 为小数位数 | round(3.14159, 2) 返回 3.14 |
示例:
python
# 内置数学函数示例
print(abs(-5)) # 输出:5
print(pow(2, 3)) # 输出:8
print(max(1, 2, 3, 4, 5)) # 输出:5
print(min(1, 2, 3, 4, 5)) # 输出:1
print(round(3.14159, 2)) # 输出:3.14
print(round(3.14159)) # 输出:3数学模块
Python 的 math 模块提供了更多的数学函数。
示例:
python
# 数学模块示例
import math
print(math.pi) # 输出:3.141592653589793
print(math.e) # 输出:2.718281828459045
print(math.sqrt(16)) # 平方根,输出:4.0
print(math.pow(2, 3)) # 幂运算,输出:8.0
print(math.floor(3.9)) # 向下取整,输出:3
print(math.ceil(3.1)) # 向上取整,输出:4
print(math.sin(math.pi / 2)) # 正弦,输出:1.0
print(math.cos(0)) # 余弦,输出:1.0
print(math.tan(math.pi / 4)) # 正切,输出:0.9999999999999999
print(math.log(10)) # 自然对数,输出:2.302585092994046
print(math.log10(100)) # 以 10 为底的对数,输出:2.0
print(math.exp(1)) # e 的幂,输出:2.718281828459045随机数
Python 的 random 模块提供了生成随机数的函数。
示例:
python
# 随机数示例
import random
# 生成 0 到 1 之间的随机浮点数
print(random.random()) # 输出:0.123456789...
# 生成指定范围内的随机整数
print(random.randint(1, 10)) # 输出:1 到 10 之间的随机整数
# 从序列中随机选择一个元素
fruits = ["apple", "banana", "cherry"]
print(random.choice(fruits)) # 输出:随机选择的水果
# 打乱序列
random.shuffle(fruits)
print(fruits) # 输出:打乱后的水果列表
# 从序列中随机选择指定数量的元素
print(random.sample(fruits, 2)) # 输出:随机选择的 2 个水果类型转换
Python 支持数字类型之间的转换。
示例:
python
# 类型转换示例
# 整数转换为浮点数
x = 10
print(float(x)) # 输出:10.0
# 浮点数转换为整数
x = 3.14
print(int(x)) # 输出:3
# 整数转换为复数
x = 5
print(complex(x)) # 输出:(5+0j)
# 浮点数转换为复数
x = 3.14
print(complex(x)) # 输出:(3.14+0j)
# 字符串转换为数字
x = "123"
print(int(x)) # 输出:123
print(float(x)) # 输出:123.0
x = "3.14"
print(float(x)) # 输出:3.14
print(int(float(x))) # 输出:3数字常量
Python 中一些常用的数字常量:
math.pi:圆周率 πmath.e:自然对数的底数 emath.inf:无穷大math.nan:非数字
示例:
python
# 数字常量示例
import math
print(math.pi) # 输出:3.141592653589793
print(math.e) # 输出:2.718281828459045
print(math.inf) # 输出:inf
print(math.nan) # 输出:nan
# 检查无穷大和非数字
print(math.isinf(math.inf)) # 输出:True
print(math.isnan(math.nan)) # 输出:True总结
Python 提供了丰富的数字类型和操作,包括整数、浮点数和复数。这些数字类型支持各种数学运算,并且可以通过内置函数和模块进行更复杂的数学操作。理解 Python 中的数字类型及其操作对于编写有效的 Python 代码非常重要。