• Python3 time模块&datetime模块&random模块


     1 '''
     2 time模块
     3 '''
     4 
     5 # import time
     6 # print(help(time))       # help()提供帮助
     7 # print(time.time())      # 1970年开始到现在的秒数(时间戳)——重点
     8 # time.sleep(3)           # CPU不工作,阻塞——重点
     9 # print(time.clock())     # 计算CPU工作时间
    10 #
    11 # # 结构化(元组形式)
    12 # print(time.gmtime())    # 格里尼治天文台时间,标准时间——重点
    13 #                         # time.struct_time(tm_year=2017, tm_mon=10, tm_mday=18, tm_hour=1, tm_min=56, tm_sec=47, tm_wday=2, tm_yday=291, tm_isdst=0)
    14 # print(time.localtime()) # 本地时间——重点
    15 # a1 = time.localtime()
    16 # print(time.strftime('%Y-%m-%d  %H:%M:%S',a1))  # 自定义时间显示样式——重点
    17 # print(time.strptime('2017-09-08 10:21:10','%Y-%m-%d  %H:%M:%S'))    # 转换为结构化时间,前后的格式要一一对应,各市要相同
    18 #                                                                         # 重点
    19 # print(time.ctime())     # 当前时间(格式不能动),参数默认为空,加入参数后,将参数转换为时间,从1970年算起
    20 # print(time.mktime(time.localtime()))    # 转化为时间戳
    21 
    22 '''
    23 datetime 模块
    24 '''
    25 # import datetime
    26 # print(datetime.datetime.now())  # 显示现在时间
    27 
    28 
    29 
    30 
    31 '''
    32 random 模块
    33 '''
    34 # import random
    35 # 
    36 # print(random.random())      # 随机生成数,random()——默认范围是0-1
    37 # print(random.randint(1,8))   # 范围1-8
    38 # print(random.choice('hello'))   # 随机选择
    39 # print(random.choice([123,'asd',[1,2]])) # 随机选择元素
    40 # # print(random.sample([123,'asd'],1,44))  # TypeError: sample() takes 3 positional arguments but 4 were given
    41 # print(random.sample([123,'asd'],1))     # 正确
    42 # print(random.randrange(1,5))    # range 取值取不到5,范围1-4
    43 # 
    44 # 
    45 # # 验证码函数
    46 # # version--1
    47 # def rand_code():
    48 #     code = ''
    49 # 
    50 #     for i in range(5):
    51 #         if i == random.randint(0,4):
    52 #             add = random.randrange(10)
    53 #         else:
    54 #             add = chr(random.randrange(65,91))
    55 #         code += str(add)
    56 #     print(code)
    57 # rand_code()
    58 # 
    59 # # version--2
    60 # def fun1():
    61 #     code = ''
    62 #     for i in range(5):
    63 #         add = random.choice([random.randrange(10),chr(random.randrange(65,91))])
    64 #         code += str(add)
    65 #     print(code)
    66 # fun1()
  • 相关阅读:
    "科林明伦杯"哈理工第九届——分布式服务(概率期望+思维)
    Nim游戏——简单博弈论
    acwing 1252搭配购买(并查集 + 01背包 )
    [LeetCode] 67. Add Binary
    [LeetCode] 415. Add Strings
    [LeetCode] 43. Multiply Strings
    [LeetCode] 412. Fizz Buzz
    [LeetCode] 201. Bitwise AND of Numbers Range
    [LeetCode] 389. Find the Difference
    [LeetCode] 326. Power of Three + 342. Power of Four
  • 原文地址:https://www.cnblogs.com/Infi-chu/p/7685865.html
Copyright © 2020-2023  润新知