回顾
-
迭代器
- 序列
- next()
- 遍历
-
生成器
-
装饰器
''' 无参 装饰器 def 装饰器名(fn): def new_fn(*args,**kwargs): result = fn(*args,**kwargs 添加新功能 return result return new_fn '''
-
递归
''' 找临界值 找关系 递归 - 解决循环问题 栈区间不断增加 占内存 有最大深度 ''' a =234 def star(n): if n==1: print('*') return star(n-1) print('*'*n) star(5)
总结
import random
import uuid
import random
# 1.随机整数
# random.randint(m,n)-产生m 到n的随机整数
print(random.randint(10,34))
# 2.随机浮点数
# random.random() --产生0~1 的随机小数 [0,1)
print(random.random()) #[0,1)
print(random.random()*100) #[0,100)
print(random.random()*80 + 20) #[20,100)
# 3.洗牌 随机打乱列表中元素的顺序
# random.shuffle(列表)
nums = [13,234,5,5,6,7,68]
random.shuffle(nums)
print(nums) #[7, 13, 234, 6, 5, 68, 5]
# 4.
# random.choice(序列) --随机获取序列中的一个元素
# random.choices(序列,k=N) --随机获取序列中N个元素
print(random.choice(nums)) #13
print(random.choices(nums, k=3)) #[5, 5, 7]
# 随机
# 产生唯一文本信息
import uuid
print(uuid.uuid1()) #53e80958-b488-11eb-bfa3-34c93d8cda83
print(uuid.uuid4()) #731bcaff-dfcf-486c-a6b0-f786d7959694
print(uuid.uuid4())
时间戳
'''
时间戳是通过保存某个时间点到1970年1月1日0时0分0秒(格林威治时间,有八个小时时差,python中不用管)的时间差来保存时间的,
单位是秒
保存时间戳比保持字符串时间更节约内存。
节约内存 二进制八位一个字节
加密解密时间简单 #1620978958.7355142 + 10000
'''
'''
时间戳是通过保存某个时间点到1970年1月1日0时0分0秒(格林威治时间,有八个小时时差,python中不用管)的时间差来保存时间的,
单位是秒
保存时间戳比保持字符串时间更节约内存。
节约内存 二进制八位一个字节
加密解密时间简单 #1620978958.7355142 + 10000
'''
import time
# 1.time.time() --- 获取当前时间的时间戳
t1 = time.time()
print(t1) #1620978958.7355142 年 月日时 分秒 星期
# 2.time.localtime()--获取当地时间 ,返回的是结构体时间
# time.localtime(时间戳) --将时间戳转换成结构体时间
t2 = time.localtime()
print(t2)
print(t2.tm_year,t2.tm_mon,t2.tm_mday)
t3 = time.localtime(0)
print(t3)
# 3.time.sleep(N) -睡眠指定时间(单位是秒)
print('======')
# time.sleep(3)
print('++++++')
# ==========================================================
import datetime
from datetime import datetime,time,date,timedelta
#
# 获取当前时间
t1 = datetime.now() #2021-05-14 16:46:07.115770
print(t1,type(t1))
print(t1.year,t1.month,t1.day)
t2 = date.today()
print(t2) #2021-05-14
# 2.获取时间对应的时间戳
# datetime对象.timestamp()--获取时间对象对应的时间戳
t3 = t1.timestamp()
print(t3)
# 3.将字符串时间转换成时间对象
# datetime.strptime (时间字符串,时间格式字符串)
'''
%Y - 年
%m - 月
%d -日
%H- 时:24
%M- 分
%S- 秒
'''
t_str = '2015-3-4 8:34:45'
t4 = datetime.strptime(t_str,'%Y-%m-%d %H:%M:%S')
print(t4,type(t4))
print(t4.timestamp())
t_str1 = '2020/5/4'
t5 = datetime.strptime(t_str1,'%Y/%m/%d')
print(t5) #2020-05-04 00:00:00
# 4.日期的加减操作 只能减天时分秒 周
t_str = '2015-12-31 23:59:59'
t4 = datetime.strptime(t_str,'%Y-%m-%d %H:%M:%S')
print(t4 + timedelta(seconds=1)) #2016-01-01 00:00:00
print(t4 + timedelta(days=2))
print(t4 + timedelta(hours=3))