在Python3中有三类基本的数值类型:int、float和complex。此外bool型为int的子类。
1. int类型
int类型为有符号整数类型,在Python3中其取值范围没有限制,可用于存储二进制、八进制、十进制和十六进制整型数据。
1 2 3 4 5 6 7 8 9 10 |
int_bin= 0b11 # 二进制 int_oct= 0o11 # 八进制 int_dec= 11 # 十进制 int_hex= 0x11 # 十六进制 print(int_bin, int_oct, int_dec, int_hex) # 3 9 11 17 big_int= 123456677883345349234850248540234985029486504347982 print(big_int) # 123456677883345349234850248540234985029486504347982 ng_big_int= -392384798374523759875298347528934798237419837491824412 print(ng_big_int) # -392384798374523759875298347528934798237419837491824412 |
2. float类型
浮点数通常使用C语言中的double类型实现,用于表示双精度浮点数,其精度通常与所在平台有关,可以通过 sys.float_info 来查看。
1 2 3 4 5 6 7 8 9 10 |
JackChendeMacBook-Pro:~ jackchen$ uname -spm Darwin x86_64 i386 JackChendeMacBook-Pro:~ jackchen$ python3 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.float_info sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1) >>> |
3. complex类型
complex为复数类型,由实数部分和虚数部分构成,两部分都是浮点数。
1 2 3 4 |
c1= 1 + 2j c2= 0j c3= complex(1.1, 2.123) print(c1, c2, c3) # (1+2j) 0j (1.1+2.123j) |
4. 数据类型转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 其它进制整数转十进制整数(默认自动转换) int_bin= int(0b10) int_oct= int(0o10) int_dec= int(10) int_hex= int(0x10) print(int_bin, int_oct, int_dec, int_hex) # 2 8 10 16 # 解析其它进制的字符串 str_bin= int('0b10', base=2) str_oct= int('0o10', base=8) str_dec= int('10', base=10) str_hex= int('0x10', base=16) print(str_bin, str_oct, str_dec, str_hex) # 2 8 10 16 # 十进制转其它进制 bin_int= bin(10) oct_int= oct(10) dec_int= int(10) hex_int= hex(10) print(bin_int, oct_int, dec_int, hex_int) # 0b1010 0o12 10 0xa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# 整型数据构建 int_int= 123 int_float= int(1.23) int_str= int('1234') print(int_int, int_float, int_str) # 123 1 1234 # 浮点数类型构建 float_float= 1.23 float_int= float(123) float_str= float('1.234') print(float_float, float_int, float_str) # 1.23 123.0 1.234 # 复数类型构建 complex1= 1.1 + 2.2j complex2= 2.2j complex3= complex(1.1, 2.2) complex4= complex(2.2) print(complex1, complex2, complex3, complex4) # (1.1+2.2j) 2.2j (1.1+2.2j) (2.2+0j) |
5. 常用的数值运算操作
表达式 | 说明 |
x + y | 计算x与y之和 |
x – y | 计算x与y之差 |
x * y | 计算x与y的乘积 |
x / y | 计算x与y之商 |
x // y | 计算x与y之商,并向下取整 |
x % y | 计算x除y的余数 |
-x | 取x的负数 |
abs(x) | 取x的绝对值 |
int(x) | 将x转化为整数 |
float(x) | 将x转化为浮点数 |
complex(re, im) | 构建一个复数 |
c.conjugate() | 求复数c的共轭复数 |
divmod(x, y) | 计算x除y的商和余数,等效于(x/y, x%y) |
pow(x, y) | 计算x的y次方 |
x ** y | 计算x的y次方 |
6. 常用的数学函数
函数 | 说明 |
abs(x) | 返回数字的绝对值,如abs(-10) 返回 10 |
math.ceil(x) | 返回数字的上入整数,如math.ceil(4.1) 返回 5 |
math.exp(x) | 返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045 |
math.fabs(x) | 返回数字的绝对值,如math.fabs(-10) 返回10.0 |
math.floor(x) | 返回数字的下舍整数,如math.floor(4.9)返回 4 |
math.log(x) | 如math.log(math.e)返回1.0,math.log(100,10)返回2.0 |
math.log10(x) | 返回以10为基数的x的对数,如math.log10(100)返回 2.0 |
max(x1, x2,…) | 返回给定参数的最大值,参数可以为序列。 |
min(x1, x2,…) | 返回给定参数的最小值,参数可以为序列。 |
math.modf(x) | 返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。 |
pow(x, y) | x**y 运算后的值。 |
round(x [,n]) | 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。 |
math.sqrt(x) | 返回数字x的平方根。 |
random.random() | 随机生成下一个实数,它在[0,1)范围内。 |
math.sin(x) | 返回的x弧度的正弦值。 |
math.cos(x) | 返回x的弧度的余弦值。 |
math.tan(x) | 返回x弧度的正切值。 |
math.asin(x) | 返回x的反正弦弧度值。 |
math.acos(x) | 返回x的反余弦弧度值。 |
math.atan(x) | 返回x的反正切弧度值。 |
math.degrees(x) | 将弧度转换为角度,如degrees(math.pi/2) , 返回90.0 |
math.radians(x) | 将角度转换为弧度 |
math.pi | 数学常量 pi(圆周率,一般以π来表示) |
math.e | 数学常量 e,e即自然常数(自然常数)。 |