ms
#7 24
import datetime import time # print(time.ctime()) #Wed Jul 24 21:58:39 2019 print(time.localtime()) # struct_time print(time.time()) #1563976743.373683 print(datetime.date.today()) #2019-07-24 print(datetime.datetime.today()) #2019-07-24 22:04:07.553081 print(datetime.datetime.now()) #2019-07-24 22:04:07.553081 tm3 = datetime.datetime.strptime("2018-01-16 23:44:55", "%Y-%m-%d %H:%M:%S") print(tm3.year) 时间戳 结构化 字符串 time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(stamp)) 字符串 结构化 时间戳 time.mktime(time.strptime('2019-9-1 9:9:1',"%Y-%m-%d %H:%M:%S")) lst = [1,2,3,4,5] import random print(random.shuffle(lst)) #没有返回值 None 随机打乱
import threading import time flag = 0 class MyThread(threading.Thread): def __init__(self,name,counter): threading.Thread.__init__(self) self.name = name self.counter = counter def run(self): print('%s'%self.name,'开始') fn(self.name,self.counter ,5) #把要执行的代码写到run函数里面 线程在创建后会直接运行run函数 print('%s' % self.name, '结束') def fn(threadName, delay, counter): while counter: #5 4 3 2 1 延迟1/2秒 if flag: threadName.exit() time.sleep(delay) print("%s: %s" % (threadName, time.ctime(time.time()))) counter -= 1 t1 = MyThread('t1',1) t2 = MyThread('t2',2) t1.start() t2.start() t1.join() t2.join() print('线程结束') # t1 开始 # t2 开始 # t1: Thu Jul 25 10:12:36 2019 1 # t1: Thu Jul 25 10:12:37 2019 2 # t2: Thu Jul 25 10:12:37 2019 -1- # t1: Thu Jul 25 10:12:38 2019 3 # t1: Thu Jul 25 10:12:39 2019 4 # t2: Thu Jul 25 10:12:39 2019 -2- # t1: Thu Jul 25 10:12:40 2019 5 # t1 结束 # t2: Thu Jul 25 10:12:41 2019 -3- # t2: Thu Jul 25 10:12:43 2019 -4- # t2: Thu Jul 25 10:12:45 2019 -5- # t2 结束 # 线程结束 t1延迟1秒 t2延迟2秒