zoukankan      html  css  js  c++  java
  • 【Rollo的Python之路】python random函数与time 函数

    random() 方法返回随机生成的一个实数,它在[0,1)范围内。

    import random
    
    print(random.random())  #随机生成一个0-1的数字

    注意:random()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。

    import random
    
    print(random.randint(1,9)) #随机一个大于1小于9的小数,randint里面是整数范围
    
    print(random.randrange(1,9,2) ) #随机一个大于1小于9的奇数,2表示递增基数
    import random
    
    print(random.choice(['123','abc',52,[1,2]]))    #随机返回参数列表中任意一个元素
    print(random.sample(['123','abc',52,[1,2]],3)) #随机返回参数,3表示返回几个
    
    #执行结果:
    
    52
    ['abc', [1, 2], 52]
    import random
    
    lis = [1,2,5,7,9,10]
    random.shuffle(lis)  #打乱顺序
    print(lis)
    
    
    #执行结果:
    
    [1, 7, 2, 10, 9, 5]
    import random
    
    random.uniform(1, 10)
    
    #执行结果:
    
    随机浮点数

    练习:随机生成验证码

    import random
    
    def qr_code():
        code=""
        for i in range(5):
            if i == random.randint(0,9):
                add = random.randrange(10)
            else:
                add = chr(random.randrange(65,91))
            code += str(add)
        print(code)
    
    qr_code()

    高级版:

    import random
    
    def qr_code():
        code=""
        for i in range(5):
            add = random.choice([random.randrange(10),chr(random.randrange(65,91))])
            code += str(add)
        print(code)
    
    qr_code()

    time函数:

    1.时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日开始按秒计算的偏移量(time.gmtime(0))此模块中的函数无法处理1970纪元年以前的时间或太遥远的未来(处理极限取决于C函数库,对于32位系统而言,是2038年)
    
    2.UTC(Coordinated Universal Time,世界协调时)也叫格林威治天文时间,是世界标准时间.在我国为UTC+8
    
    3.DST(Daylight Saving Time)即夏令时
    
    4.一些实时函数的计算精度可能不同

    time模块的格式:

    1.0 timestamp,通常说法:时间戳。

    时间戳表示的是从1970年1月1日开始按秒计算的偏移量(time.gmtime(0))此模块中的函数无法处理1970纪元年以前的时间或太遥远的未来

    2.0 格式化的时间字符串(Format String):2019-05-03 

    %y 两位数的年份表示(00-99%Y 四位数的年份表示(000-9999%m 月份(01-12%d 月内中的一天(0-31%H 24小时制小时数(0-23%I 12小时制小时数(01-12%M 分钟数(00=59%S 秒(00-59%a 本地简化星期名称
    %A 本地完整星期名称
    %b 本地简化的月份名称
    %B 本地完整的月份名称
    %c 本地相应的日期表示和时间表示
    %j 年内的一天(001-366%p 本地A.M.或P.M.的等价符
    %U 一年中的星期数(00-53)星期天为星期的开始
    %w 星期(0-6),星期天为星期的开始
    %W 一年中的星期数(00-53)星期一为星期的开始
    %x 本地相应的日期表示
    %X 本地相应的时间表示
    %Z 当前时区的名称
    %% %号本身

    3.0 struct_time

    time 的内置函数

    1.0 Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数)。

    time()方法语法:

    time.time()
    import time
    
    print(time.time())  #时间戳
    
    #执行结果:
    
    1556885145.0857482

    2.0 time.localtime()

    import time
    
    print(time.localtime())
    
    
    #执行结果:
    
    time.struct_time(tm_year=2019, tm_mon=5, tm_mday=3, tm_hour=20, tm_min=9, tm_sec=53, tm_wday=4, tm_yday=123, tm_isdst=0)
    
    #tm_year  年,tm_mon 月, tm_mday 日, tm_hour=小时, tm_min=分, tm_sec=秒, tm_wday=周(0-6), tm_yday=一年中的第几天, tm_isdst=

    3.0 time.clock():cpu工作时间

    import time
    
    print(time.clock())
    
    0.115648466
    DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
      print(time.clock())

    4.0 time.sleep(): 睡眠时间

    5.0 time.ctime():

    import time
    
    print(time.ctime())
    
    #执行结果:
    
    Fri May  3 20:19:46 2019

    6.0 time.strftime()

    import time
    
    print(time.strftime("%Y--%m--%d %H:%M:%S %a"))
    
    #执行结果:
    
    2019--05--03 20:23:24 Fri

    7.0 time.gmtime():UTC时间

    import time
    
    print(time.gmtime())
    
    #执行结果:
    
    time.struct_time(tm_year=2019, tm_mon=5, tm_mday=3, tm_hour=12, tm_min=24, tm_sec=38, tm_wday=4, tm_yday=123, tm_isdst=0)

    8.0 time.strptime():把一个格式化时间字符串转化为struct_time,实际上它和strftime()是逆操作

    print(time.strptime("2019-05-03","%Y-%m-%d"))
    
    #执行结果:
    
    time.struct_time(tm_year=2019, tm_mon=5, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=123, tm_isdst=-1)

    9.0 time.mktime()

    接受时间元组并返回时间辍(1970纪元年后经过的浮点秒数)

  • 相关阅读:
    Structure Boundary Preserving Segmentation for Medical Image With Ambiguous Boundary(理解)
    Deep Gamblers: Learning to Abstain with Portfolio Theory(理解)(github代码)
    DeepSurv: Personalized Treatment Recommender System Using A Cox Proportional Hazards Deep Neural Network(理解)(github代码)
    Convolutional LSTM Network: A Machine Learning Approach for Precipitation Nowcasting(理解)(github代码)
    【分类算法】朴素贝叶斯(Naive Bayes)
    【分类算法】感知机(Perceptron)
    【集成模型】Boosting
    【集成模型】Stacking
    【集成模型】Bootstrap Aggregating(Bagging)
    数据科学导引(大纲)
  • 原文地址:https://www.cnblogs.com/rollost/p/10806179.html
Copyright © 2011-2022 走看看