python进阶宝典15

发布时间:2024-11-19 04:28

在宝宝自主进食阶段,鼓励他们自己抓取食物。 #生活技巧# #育儿建议# #宝宝辅食食谱#

一   主要内容包含time.time(), time.sleep(),及 datetime模块等内容,具体详见代码注释。

import time

def calcProd():

product = 1

for i in range(1,100000):

product = product * i

return product

startTime = time.time()

prod = calcProd()

endTime = time.time()

print('The result is %s digits long.' % (len(str(prod))))

print('Took %s seconds to calculate.' % (endTime - startTime))

import time

time.sleep(30)

for i in range(30):

time.sleep(1)

二 下面是一个例子描边,每一次按下回车后显示当前一轮的时间,及累积时间。

import time

print('Press ENTER to begin. Afterwards,press ENTER to "click" the stopwatch.Press Ctrl-C to quit.')

input()

print('Started.')

startTime = time.time()

lastTime = startTime

lapNum = 1

try:

while True:

input()

lapTime = round(time.time() - lastTime, 2)

totalTime = round(time.time() - startTime, 2)

print('Lap #%s: %s (%s)' % (lapNum,totalTime,lapTime),end='')

lapNum += 1

lastTime = time.time()

except KeyboardInterrupt:

print('\nDone.')

输出结果如下:


三  以下代码主要包含以下功能

1) datetime模块,datetime值表示一个具体的时刻

2)timedelta数据类型 ,表示一段时间

3)将datetime对象转换为字符串 strftime()

4)将字符串转换成 datetime对象 strptime()

具体代码如下:

import datetime

datetime.datetime.now()

print(datetime.datetime.now())

dtnow = datetime.datetime.now()

dt = datetime.datetime(2015,10,21,16,29,0)

print(dt)

print(dtnow > dt)

print(datetime.datetime.fromtimestamp(1000000))

datetime.datetime.fromtimestamp(time.time())

delta = datetime.timedelta(weeks=2,days=11,hours=10,minutes=9,seconds=8)

print(delta.days,delta.seconds,delta.microseconds)

print(delta.total_seconds())

str(delta)

dt = datetime.datetime.now()

kdays = datetime.timedelta(days=1000)

kdays = datetime.timedelta(days=500 * 2)

print(dt + kdays)

print(dt - (2 * kdays) )

import datetime,time

t2018 = datetime.datetime(2018,10,31,0,0,0)

t = 1

try:

while datetime.datetime.now() < t2018 :

time.sleep(1)

t += 1

except KeyboardInterrupt:

print('%s seconds have gone.' % (t))

print('Time is ok,Go...')

import datetime

dt = datetime.datetime(2017,4,21,16,29,0)

dt.strftime('%Y/%m/%d %H:%M:%S')

dt.strftime("%B of '%y %I:%M %p , %A , %w th day of week, %j th day of year")

import datetime

datetime.datetime.strptime('2018/05/20 16:30:00','%Y/%m/%d %H:%M:%S')


网址:python进阶宝典15 https://www.yuejiaxmz.com/news/view/130313

相关内容

python 列表转为字典的两个小方法
Python 量化投资原来这么简单(5) — A股回测KDJ策略
python
Python实现经典还钱问题算法:优化财务管理的编程技巧
解码支付宝交易安全:揭秘Python验签的实战技巧与风险防范
什么是育儿宝典
【Python】Python连接Hadoop数据中遇到的各种坑(汇总)
Python笔记——Python中is和==的区别
python刷题(25
幼儿中班育儿宝典大全

随便看看