数值的格式化处理

‘{:.2f}’.format(2/4)  保留2为小数

‘{:.2%}’.format(2/4)  百分数保留两位小数

math.ceil(number)向上取值

math.trunc(number)截断小数

math.floor(number)向下取值

round(number,2)四舍五入

随机数

Import random

List=[1,2,3,4,5,6]

random.choice(list)从列表中随机取一个数

random.sample(list,3)从列表中随机取3个数

random.shuffle(list)对列表中的数值进行乱序处理

random.randint(1,10)1到10中随机取一个整数

random.random()0到1中随机取一个浮点数

日期与时间的常用操作

Import datetime

today=datetime.date.today()  tody.year, tody.month, today.day, today.weekday(), today.isoweekday()

birthday=datetime.date(1988,03,03)

time=datetime.time(17,14,33) time.hour, time.minute, time.second

now=datetime.datetime.now() now.year, now.second

create_time=datetime.datetime(1990,3,3,20,33,55)

str_date=’2019-08-19’

strp_date=datetime.datetime.strptime(str_date,’%Y-%m-%d’)字符串转换为日期

strf_date=strp_date.strftime(‘%Y/%m%d’)日期转换为字符串

caculate_date=now+datetime.timedelta(days=100)

posted on 2019-08-19 18:11  *赤子之心*  阅读(225)  评论(0)    收藏  举报