• 常用的python内置模块


    1、time模块:

    time模块是普通的时间模块

    在python的三种时间表现形式:
    1.时间戳: 给电脑看的。
    - 自1970-01-01 00:00:00到当前时间,按秒计算,计算了多少秒。

    2.格式化时间(Format String): 给人看的
    - 返回的是时间的字符串 2002-01-11

    3.格式化时间对象(struct_time):
    - 返回的是一个元组, 元组中有9个值:
    9个值分别代表: 年、月、日、时、分、秒、一周中第几天,一年中的第几天,夏令时(了解)

    1)获取时间戳:
    import time
    
    # 获取时间戳(******)计算时间时使用
    print(time.time())  #1573895872.453043 ,给电脑看的

    2)格式化时间:

    # 获取年月日
    print(time.strftime('%Y-%m-%d'))  #2019-11-16
    
    # # 获取年月日时分秒
    print(time.strftime('%Y-%m-%d %H:%M:%S')) #2019-11-16 17:20:15
    # # %X == %H:%M:%S
    print(time.strftime('%Y-%m-%d %X')) #2019-11-16 17:20:15
    
    # 获取年月
    print(time.strftime('%Y/%m')) #2019/11

    3) 获取时间对象:

    print(time.localtime())  #time.struct_time(tm_year=2019, tm_mon=11, tm_mday=16, tm_hour=17, tm_min=23, tm_sec=36, tm_wday=5, tm_yday=320, tm_isdst=0)
    print(type(time.localtime())) #<class 'time.struct_time'>
    time_obj = time.localtime()
    print(time_obj.tm_year)
    print(time_obj.tm_mon)

    将时间对象转为格式化时间:

    import datetime
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())

    将字符串格式的时间转为时间对象:

    import time
    res = time.strftime('2019-01-01, %Y-%m-%d')
    print(res)

    2、datetime 模块

    datetime 模块是基于time模块封装的一种比较便利的时间模块

    import datetime
    
    # 获取当前年月日
    print(datetime.date.today())
    
    # 获取当前年月日时分秒
    print(datetime.datetime.today())
    
    time_obj = datetime.datetime.today()
    print(type(time_obj))
    print(time_obj.year)
    print(time_obj.month)
    print(time_obj.day)
    #UTC
    print(time_obj.weekday())  # 0-6
    #ISO
    print(time_obj.isoweekday())  # 1-7
    
    #UTC时区
    #北京时间
    print(datetime.datetime.now()) #格林威治
    print(datetime.datetime.utcnow())
    日期/时间的计算:
    日期时间 = 日期时间 “+” or “-” 时间对象
    时间对象 = 日期时间 “+” or “-” 日期时间
    # 日期时间:
    current_time = datetime.datetime.now()
    print(current_time)
    
    # 时间对象
    # 获取7天时间
    time_obj = datetime.timedelta(days=7)
    print(time_obj)
    
    # 获取当前时间7天后的时间
    # 日期时间 = 日期时间 “+” or “-” 时间对象
    later_time = current_time + time_obj
    print(later_time)
    
    # 时间对象 = 日期时间 “+” or “-” 日期时间
    time_new_obj = later_time - current_time
    print(time_new_obj)

    3、random 模块:

    random模块主要用于随机获取指定的值

    随机获取1—9中任意的整数:

    import random
    res = random.randint(1, 9)
     print(res)

    默认获取0——1之间任意小数

    res2 = random.random()
    print(res2)

    洗牌:

    #random.shuffle()可以对某个有索引的可迭代对象进行乱序。

    需要注意的是:不可变类型和无序数据类型不能乱序

    # 将可迭代中的值进行乱序
    list1 = ['红桃A', '梅花A', '红桃Q', '方块K']
    random.shuffle(list1)  
    print(list1)

    随机获取可迭代对象中的某一个值:

    list1 = ['红桃A', '梅花A', '红桃Q', '方块K']
     res3 = random.choice(list1)   #随机获取一个值
     print(res3)

    具体例题:

    # 需求: 随机验证码
    '''
    需求:
    大小写字母、数字组合而成
    组合5位数的随机验证码
    ''

    import random
    
    def get_code(n):
        code1 = ''
        for line in range(n):
            # 随机生成一个小字母
            res1 = random.randint(97, 122)
            lower_str = chr(res1)  # 根据ASCII码数字对应小字母
            # 随机生成一个大字母
            res2 = random.randint(65, 90)
            upper_str = chr(res2)  # 根据ASCII码数字对应大字母
            # 随机生成一个数字
            res3 = random.randint(0, 9)  # 随机生成一个整数
            number = str(res3)
            code_list = [lower_str, upper_str, number]
            code_choice = random.choice(code_list)  # 从列表中随机去一个元素
            code1 += code_choice
        print(code1)
        print(len(code1))
        return code1
    
    
    get_code(10)

    注:在ASCII码中,数字范围:97-122,对应小写字母;数字范围65-90,对应大写字母。

  • 相关阅读:
    初等数论及其应用——Lucas定理
    数据结构编程实验——chapter10-应用经典二叉树编程
    Coursera课程 Programming Languages 总结
    Coursera课程 Programming Languages, Part C 总结
    读《如何阅读一本书》有感
    Educational Codeforces Round 34
    Coursera课程 Programming Languages, Part B 总结
    Codeforces #451 Div2 F
    Codeforces #452 Div2 F
    Coursera课程 Programming Languages, Part A 总结
  • 原文地址:https://www.cnblogs.com/xy-han/p/11894552.html
Copyright © 2020-2023  润新知