常用内置模块

常用内置模块之collections

1.具名元组:namedtuple

namedtuple: 生成可以使用名字来访问元素内容的tuple
from collections import namedtuple

  # 表示二位坐标系
    point = namedtuple('点', ['x', 'y'])
    # 生成点信息
    p1 = point(1, 2)  # 点(x=1,y=2)
    print(p1.x)  # 1
    print(p1.y)  # 2

    card = namedtuple('扑克牌', ['num', 'color'])
    c1 = card('A', '黑♠')
    c2 = card('A', '红♥')
    print(c1.num, c1.color)  # A 黑♠
    print(c2.num, c2.color)  # A 红♥

2.双端队列deque

队列
队列与堆栈
队列:先进先出
堆栈:先进后出
队列和堆栈都是一边只能进一边只能出
使用list存储数据时,按索引访问元素很快,但是插入和删除元素就很慢了,因为list是线性存储,数据量大的时候,插入和删除效率很低

deque是为了高效实现插入和删除操作的双向俩表,适合用于队列和栈

from collections import deque
q = deque(['a','b','c'])
q.append('x')
q.append('y')
print(q)  # deque(['y', 'a', 'b', 'c', 'x'])

常用内置模块之时间模块

    import time 
    """
    三种时间表现形式
    1.时间戳(秒数)
    2.结构化时间(主要是给计算机看的)
    3.格式化时间(主要给人看的)
    """
常用方法
    1.time.sleep(secs)
    (线程)推迟指定的时间运行,单位为秒。
    2.time.time()
    获取当前时间戳

time模块之strftime

print(time.time())  # 获得时间戳
print(time.localtime())  # 获得结构化时间
print(time.strftime('%Y-%m-%d'))  # 获得年月日2022-10-19
print(time.strftime('%Y/%m/%d'))  # 年必须大写2022/10/19
print(time.strftime('%Y-%m-%d %H:%M:%S'))  # 时分秒2022-10-19 15:59:24
'''
datetime 年月日 时分秒
date     年月日
time     时分秒(后续会有此规律)
'''

元组(struct_time) :struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)

索引(Index) 属性(Attribute) 值(Values)
0 tm_year(年) 比如2011
1 tm_mon(月) 1 – 12
2 tm_mday(日) 1 – 31
3 tm_hour(时) 0 – 23
4 tm_min(分) 0 – 59
5 tm_sec(秒) 0 – 60
6 tm_wday(weekday) 0 – 6(0表示周一)
7 tm_yday(一年中的第几天) 1 – 366
8 tm_isdst(是否是夏令时) 默认为0

几种格式之间的转换image

datetime模块

from datetime import date, datetime

print(date.today())  # 现在的日期 2022-10-19
print(datetime.today())  # 今日的具体时间 2022-10-19 16:18:31.114935
print(datetime.utcnow())   # UTC时间
"strptime"将时间按照后面格式化输出
from datetime import datetime

d = datetime.strptime('2017/9/30', '%Y/%m/%d')
print(d)  # 2017-09-30 00:00:00
e = datetime.strptime('2017年9月30日星期六8时42分24秒', '%Y年%m月%d日星期六%H时%M分%S秒')
print(e)  # 2017-09-30 08:42:24
'timedelta()' 增加时间,括号内可以填写月,天,时分秒'
import datetime
ctime = datetime.date.today()
print(ctime)  # 2022-10-19
time_del = datetime.timedelta(days=3)  #时间向后增加三天
print(ctime + time_del)  # 2022-10-22 
ctime = datetime.datetime.today()
print(ctime)  # 2022-10-19 16:36:37.675843
time_del = datetime.timedelta(minutes=20)  # 时间增加20分钟
print(ctime + time_del)  # 2022-10-19 16:56:37.675843

常用内置模块之随机数模块

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))
# 随机抽取指定的样本个数,2个['jerry', 'tom']
l1 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
random.shuffle(l1)
print(l1)  # 随机打乱数据集

面试题:产生图片验证码: 每一位都可以是大写字母 小写字母 数字 4位

"""产生图片验证码: 每一位都可以是大写字母 小写字母 数字  4位"""
def get_code(n):  # 定义一个函数,有形参,可以传参
    code = ''  # code定义一个空的字符串
    for i in range(n):  # for循环可以自动结束,表示下面三个随机产生的可以产生几次
        # 先随机产生大写字母,小写字母,数字
        random_upper = chr(random.randint(65, 90))  # 大些字母:A-Z,随机产生
        random_lower = chr(random.randint(97, 122))  # 小写字母:a-z,随机产生
        random_int = str(random.randint(0, 9))  # 随机产生0到9
        # print(random_upper,random_int,random_lower)
        # 2.随机三选1
        temp = random.choice([random_upper, random_lower, random_int])  # 随机在产生的大小写数字里面产生
        code += temp  # 字符串里面添加随机产生的大小写数字
    return code  # 返回随机产生的验证码


res = get_code(4)  # res用来接收随机产生的验证码
print(res)

原文地址:http://www.cnblogs.com/zhanghong1229/p/16807360.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性