常见的内置模块
包的具体使用
导入包里面的模块可以使用 from ... import ... 如果使用import ... 导入包时,则包名下必须包含__init__.py文件 该文件内有什么名字就可以通过包名点出来
编程思想转变
1.面条版 2.函数版 3.模块版 4.面向对象版
软件开发目录规范
# 文件夹 bin start.py # 存储文件的启动文件 conf settings.py # 存储程序的配置文件 lib common.py # 存储程序的公共能力 core src.py # 存储程序的核心逻辑 db userinfo.txt # 存储程序的数据文件 log log.log # 存储程序的日志文件 interface user.py/order.py/goods.py # 存储程序的接口文件 # 文件 readme # 用于编写程序的说明,介绍,广告,产品说明书 requirements.txt # 用于存储程序所需的第三方模块名称和版本 # 以上规范在编写软件时可以不完全遵守 1. bin文件下的start.py也能直接放到项目的根目录 2. db文件夹后期项目会被数据库代替 3. log文件夹后续也会被专门的日志服务代替
常见的内置模块
collecttions模块
# 1.具名元组 namedtuple from collections import namedtuple Point = namedtuple('二维坐标系',['x','y']) res1 = Point(6, 8) res2 = Point(15,) print( res1,res2) # 二维坐标系(x=6, y=8) 二维坐标系(x=15, y=23) print(res1.x) # 6 print(res2.y) # 23
from collections import namedtuple
Point = namedtuple('三维坐标系', ['x', 'y', 'z'])
res1 = Point(1, 2, 3)
print(res1) # 直接输出变量名绑定的关系,三维坐标系的坐标会被打印
print(res1.z)
from collections import namedtuple p = namedtuple('扑克牌', ['花色', '点数']) res1 = p('♦', 'K') res2 = p('♣', '3') print(res1) print(res2) # 输出变量名就会答应扑克牌的花色和点数
# 2.双端队列 deque
from collections import deque
func = deque() func.append(1) # 右侧添加 func.append(2) func.append(3) func.appendleft(4) # 左侧添加 print(func)
# deque是为了高效实现插入和删除操作的双向列表,适用于队列和栈
队列与堆栈
队列:先进先出
堆栈:先进后出
队列和堆栈都是一边只能进一边只能出
# deque还可以实现pop()
# 3.有序字典 OrderedDict from collections import OrderedDict func = dict([('tom',1),('jerry',2)]) print(func)
# 4.默认值字典 defaultdict # 有集合 [1,4,7,13,16,18,24] # 将所有大于13值保存到字典的的第一个key里,大于13的保存到第二个key里 # {'k1':[], 'k2':[]} from collections import defaultdict res = defaultdict(k1=[],k2=[]) print(res)
# 5.简单的计数器 Counter l = '12432412314623462341345234232131425124' from collections import Counter l1 = Counter(l) print(l1)
time时间模块
时间的三种格式:
#导入时间模块 >>>import time #时间戳 >>>time.time() 1500875844.800804 #时间字符串 >>>time.strftime("%Y-%m-%d %X") '2017-07-24 13:54:37' >>>time.strftime("%Y-%m-%d %H-%M-%S") '2017-07-24 13-55-04' #时间元组:localtime将一个时间戳转换为当前时区的struct_time time.localtime() time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24, tm_hour=13, tm_min=59, tm_sec=37, tm_wday=0, tm_yday=205, tm_isdst=0)
#时间戳-->结构化时间 #time.gmtime(时间戳) #UTC时间,与英国伦敦当地时间一致 #time.localtime(时间戳) #当地时间。例如我们现在在北京执行这个方法:与UTC时间相差8小时,UTC时间+8小时 = 北京时间
#结构化时间-->时间戳 #time.mktime(结构化时间)
#结构化时间-->字符串时间 #time.strftime("格式定义","结构化时间") 结构化时间参数若不传,则显示当前时间
#字符串时间-->结构化时间 #time.strptime(时间字符串,字符串对应格式)
datetime模块
与time时间类似
import datetime
res = datetime.datetime.today() # 年月日时分秒(北京时间) res1 = datetime.date.today() # 年月日 print(res) print(res1) print(res.year) # 只要年份 # timedate里面可以放置多种参数,没有的时间可以通过换算得到 res2 = datetime.timedelta(days = 3) # 设置时间的时间差(3天) print(res1-res2) # 计算减去时间差的时间 print(datetime.datetime.utcnow()) # 东零区时间
random随机数模块(该模块可用于生成验证码)
import random print(random.random()) # 随机产生0到1之间的小数 print(random.randint(1, 6)) # 随机产生1到6之间的整数 print(random.randrange(1, 100, 2)) # 随机产生指定的整数 print(random.choice(['一等奖', '二等奖', '三等奖', '谢谢惠顾'])) # 随机抽取一个样本 '二等奖' print(random.choices(['一等奖', '二等奖', '三等奖', '谢谢惠顾'])) # 随机抽取一个样本 ['二等奖'] print(random.sample(['jason', 'kevin', 'tony', 'oscar', 'jerry', 'tom'], 2)) # 随机抽指定样本数 ['tom', 'jerry'] l1 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A'] random.shuffle(l1) # 随机打乱数据集 print(l1)
分类:
python基础 / 模块
, python基础
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律