数据类型定义了存储的是什么数据,有什么形态,支持哪些操作。
那么数值类可以进行哪些操作呢?
1.格式化
如果想要修改数值的格式,可以使用
'{0}'.format() #0只是代表占位符
#for example
f = 3.333333
'f={0:.02f}'.format(f)#.02f表示保留两位小数点
如果要格式化多个,则占位符不能一样
f = 3.333333
m = 2.222222
'f={0:.02f},m={1:.02f}'.format(f,m)
2.比较大小,返回布尔型结果
score = 60.3
score >= 60
score !=60
3.除法
10/4 #10除以4
10//4 #结果取整
10//4.0 #结果按精度高的决定,所以结果为float
4.取整
可以使用math.floor()函数,但要注意正数和负数的区别(理解:向左取最近的整数)
import math
math.floor(3.4)
math.floor(3.9)
math.floor(-3.4)
math.trunc()函数取整向0靠近
math.trunc(3.4)
math.trunc(3.9)
math.trunc(-3.4)
round()四舍五入
round(3.1)
round(3.9)
round(-3.4)
5.字面值
如果需要用十六进制、八进制、二进制表达,但不知道该怎么写,可以用以下函数转换:
#以64为例
hex(64)
oct(64)
bin(64)
十六进制开头:0x
八进制开头:0o
二进制开头:0b