本文共 3623 字,大约阅读时间需要 12 分钟。
list1=[]for i in range(5): list1.append(lambda x:x*i) #这里的匿名函数只有定义,没有调用,把函数变量存到了list里# 判断输出print(list1[0](2)) #上边for循环运行之后,i变成了4,x=2,带入匿名函数中运行,4*2=8print(list1[2](2)) #list中存储的元素都是匿名函数,4*2=8print(list1[4](2)) #4*2
a.一个py文件就是一个模块,文件名就是模块名(如果一个模块想要被其他模块使用,模块名必须是标识符并且不是关键字)
b.一个包含__init__.py文件的文件夹就是一个包导入模块的方法:
"""导入模块的语法:import 模块名 - 导入指定模块,导入后通过'模块名.x'去使用模块中所有的全局变量from 模块名 import 变量1, 变量2, 变量3, ... - 导入指定模块,导入后可以直接使用import后面所有的变量import 模块名 as 新模块名 - 导入模块并且对模块进行重命名,使用数据时候采用:'新模块名.'from 模块名 import 变量1 as 新变量1, 变量2, 变量3, ...from 模块名 import * - 导入模块中所有的全局变量导入模块的原理:不管是通过import还是from-import,导入模块的时候都会先进入指定模块,将模块中的代码全部执行选择性的让模块中的代码在被导入的时候执行和不执行:if __name__ == '__main__': # 这个if语句外面的代码在被导入的时候会执行,里面的代码被导入的时候不会被执行 pass"""
如何导入包
'''import 包名 - 只有在自定义完__init__.py后才有用import 包名.模块名 - 导入包中指定的模块, 使用模块中的内容的格式: 包名.模块名.xfrom 包名 import 模块名1, 模块名2, ...from 包名.模块 import 变量1, 变量2,...注意:import在导入模块或者包的时候,会先检查这个模块或者包之前是否已经导入过,如果已经导入过了不再重复导入'''
当导入包或者包中的内容的时候,会自动执行包中的__init__.py文件
os.getcwdb() - 返回当前工作目录
os.listdir(path) - 返回指定目录中所有的文件或者文件夹的名字 …import random# 1) random.randint(m, n) - 产生m到n的随机整数print(random.randint(10, 20))# 2) random.random() - 产生 0 ~ 1的随机小数print(random.random()) # 0 ~ 1print(random.random() * 100) # 0 ~ 100print(random.random() * 50 + 50) # 50 ~ 100print(random.random() * 5 + 15) # 15 ~ 20# 3) random.shuffle(列表) - 将列表中元素的顺序随机打乱(洗牌)nums = [23, 34, 'abc', True]random.shuffle(nums)print(nums)# 4)# random.choice(序列) - 从序列中随机获取一个元素# random.choices(序列, k=N) - 从序列中随机获取N个元素result = random.choice('abc3lo901')print(result)nums = [23, 34, 'abc', 20, 3, 4]result = random.choices(nums, k=2)print(result)
import time# 1) time() - 获取当前时间(时间戳)t1 = time.time()print(t1) # 1615966804.79146 (4字节~5字节) # '2021-3-17 03:50:00' (18字节)# 2)# time.localtime() - 获取当前本地时间(结构体时间)# time.localtime(时间戳) - 将时间戳对应的时间转换成结构体时间t2 = time.localtime()print(t2)t3 = time.localtime(0)print(t3)t4 = time.localtime(1615966804.79146)print(t4)print(t4.tm_year, t4.tm_mon)# 3)time.sleep(时间) - 让程序睡眠指定时间,单位秒print('=======')time.sleep(1)print('+++++++')# 2. datetime - time、datetime、datefrom datetime import time, datetime, date, timedelta# 1) 获取当前时间t5 = datetime.now()print(t5) # 2021-03-17 16:36:54.297076print(t5.year, t5.month, t5.hour)t6 = date.today()print(t6) # 2021-03-17# 2) 时间的加减操作print('当前时间:', datetime.now())# 10天前:print('10天前:', t5 - timedelta(days=10)) # 2021-03-07 16:42:01.363135# 20天前print('20天前', t5 - timedelta(days=20)) # 2021-02-25 16:42:36.397193print('1个小时前:', t5 - timedelta(hours=1))print('2个小时后:', t5 + timedelta(hours=2))print('47个小时后:', t5 + timedelta(hours=47))print('2小时30分后:', t5 + timedelta(hours=2, minutes=30))t6 = t5 - timedelta(days=1, hours=1)print(t6, t6.year, t6.month, t6.day, t6.hour, t6.minute, t6.second)
hash加密的特点:a. 不可逆:无法通过hash算法加密得到密文获取到原数据b. 相同的数据通过相同的算法得到摘要(密文)是一样的c. 不同长度是数据通过相同的算法得到的摘要(密文)的长度一样
# 1) 根据算法创建hash对象: hashlib.算法名()# 常用算法:md5、shaXhash_obj = hashlib.md5()# 2)添加原文# hash对象.update(需要加密的数据)# 注意:加密数据的类型必须bytes(相当于python中二进制数据)hash_obj.update('123456'.encode())# 3)获取摘要(获取密文)# hash对象.hexdigest()result = hash_obj.hexdigest()print(result) # e10adc3949ba59abbe56e057f20f883e
转载地址:http://frrjz.baihongyu.com/