Python-数字
参考点:
科学计数法转换
分类
整数:
1,2,3,4,5,6
浮点数:
0.1,0.2,0.3
运算
加减乘除±*/
(\应用与转义符)
print(0.1*3)
结果:
0.30000000000000004#由计算机内部运算造成
遍历
range(N): 0到N-1
range(M,N): M到N-1
for i in range(5):#range:范围
print(i)
结果:
0
1
2
3
4
符号 | 代表意义 |
---|---|
x // y | x和 y的平均商 |
x % y | 取余 |
abs(x) | x的绝对值 |
float(x) | x转换为浮点数 |
int(x) | x转换为整数 |
complex(re, im) | 实部re,虚部im |
c.conjugate() | 复数c的共轭 |
divmod(x, y) | (x // y, x % y) |
pow(x, y) | x的y次幂 |
x ** y | x的y次幂 |
复杂函数运算
复杂函数运算需导入标准库或第三方库(python中pi的解决)
import math
a=math.cos(math.pi/2)#Python调用pi,a的结果为科学计算法
b='{:.5f}'.format(a)#5位浮点数
print(a)
print(b)
结果
6.123233995736766e-17
0.00000
数字格式化输出
import math
a=math.pi/2
b='{0:,.2f}'.format(a**100)#,千分位分隔符;.精度;2保留两位小数;f浮点数
c='{0:b},{0:c},{0:d},{0:o},{0:x},{0:X}'.format(123)
d='{0:e},{0:E},{0:f},{0:%}'.format(a)
print(a)
print(b)
print(c)
print(d)
结果:
1.5707963267948966
40,924,907,173,834,539,008.00
1111011,{,123,173,7b,7B
1.570796e+00,1.570796E+00,1.570796,157.079633%