Python学习笔记——格式化输出和取整

格式化输出

格式化输出之——print

print'%d' % 20#十进制
20
print'%o' % 20#八进制
24
print'%x' % 20#十六进制
14
print("%.2g" % 1.35)#两位有效数字
1.4
print("%.1f" % 1.35)#浮点数
1.3

格式化输出之——format

print('{:.2g}'.format(1.35))
1.4

print('{:.2g}%'.format(1.35))
1.4%

print('{:.2%}'.format(1.35))
135.00%

# 实战练习
b = []
data = [[0,0.922584,0.537336,0.0384615,0.0720395 ]]
b = '({:.3g},{:.3g}),({:.3g},{:.3g})'.format(*data[0][1:])
print(b)
(0.923,0.537),(0.0385,0.072)

i = 1
print(f'这样也可以{i}')
这样也可以1

a = [1,2,3]
b = ['a',5,6]
c = zip(a,b)
for x,y in c:
	print("x是{},类型是{}".format(x,type(x)))
	print("y是{},类型是{}".format(y,type(y)))
x是1,类型是<class 'int'>
y是a,类型是<class 'str'>
x是2,类型是<class 'int'>
y是5,类型是<class 'int'>
x是3,类型是<class 'int'>
y是6,类型是<class 'int'>

取整

向上取整

#需要导入math模块
import math
math.ceil(1.4)
2

向下取整

#需要导入math模块
import math
math.floor(1.4)
1

猜你喜欢

转载自blog.csdn.net/qq_36786467/article/details/109155061