python(time/random模块)

一、Time模块

1.时间戳

  • 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数
  • 最早出现的UNIX操作系统考虑到计算机产生的年代和应用的时限综合取了1970年1月1日作为UNIX TIME的纪元时间(开始时间)

2.time.time()

  • 返回当前时间的时间戳

    import time
    print time.time()
    
    -->1572350849.07

3.time.localtime()

  • 当参数为默认值时,返回本地当前时间的时间元组
  • 当输入参数秒后,返回的是1970年1月1日早上8点后+参数秒数后的时间

    import time
    
    #不带参数
    
    print time.localtime()
    
    -->time.struct_time(tm_year=2019, tm_mon=10, tm_mday=29, tm_hour=20, tm_min=12, tm_sec=47, tm_wday=1, tm_yday=302, tm_isdst=0)
    
    #带参数
    
    print time.localtime(30)
    
    -->time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=30, tm_wday=3, tm_yday=1, tm_isdst=0)

4.time.asctime()

  • 接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串
  • 默认返回当前时间

    #带参数
    
    import time
    a = time.localtime(30)        #返回为时间元组
    print time.asctime(a)
    
    #不带参数
    
    import time
    print time.asctime()

5.time.strftime()

  • 函数接收以时间元组,并返回以可读字符串表示的当地时间,格式由参数format决定
  • 时间元组参数为默认值时,返回当前格式化后的时间
  • 时间元组参数为给定值时,返回的是给定的时间元组所转化成的格式化时间

    import time
    
    #默认返回的是格式化后的时间
    print time.strftime("%Y-%m-%d %H:%M:%S")
    
    #返回的是给定的时间元组所转化成的格式化时间
    import time
    a = time.localtime(30)
    print time.strftime("%Y-%m-%d %H:%M:%S",a)
    
    #封装返回当前格式化时间的函数
    import time
    def local_time():
         return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

    python中时间日期格式化符号:

    • %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 当前时区的名称
    • %% %号本身

6.time.strptime(str,str_fmt)

  • 根据 str 的格式把时间字符串解析为时间元组

    #coding=utf-8
    
    import time
    A =  time.strftime("%Y-%m-%d %H:%M:%S")     #返回的是格式化后的时间
    print time.strptime(A,"%Y-%m-%d %H:%M:%S")  #将格式化后的时间转化为时间元组

7.time.mktime(tupletime)

  • 接收时间元组并返回时间戳

    #coding=utf-8
    
    import time
    A =  time.strftime("%Y-%m-%d %H:%M:%S")     #返回的是格式化后的时间
    B = time.strptime(A,"%Y-%m-%d %H:%M:%S")     #将格式化后的时间转化为时间元组
    print time.mktime(B)        #返回的是时间戳

8.time.sleep(s)  延时

print 1
time.sleep(3)   #暂停
print 2
time.sleep(3)
print 3

 二、Random模块

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

1.random.random()

  • 返回随机生成的一个[0,1)范围内的实数

    import random
    print random.random()
    
    -->0.112499651779

2.random.randint(x,y)

  • 随机生成 [x,y] 范围内的整数

    import random
    print random.randint(1,2)
    
    -->2

3.random.randrange(a,b,step)

  • 随机生成一个 [a,b) 之间的一个整数,可以定义 step步长
  • 与range()用法类似

    import random
    
    print random.randrange(3)
    print random.randrange(1,2)
    print random.randrange(1,6,2)

4.random.uniform(a,b)

  • 返回  [a,b] 内的一个浮点数

    import random
    
    print random.uniform(1,2)

5.random.choice(a)

  • 从 a 中随机选择一个元素,a 不能为空且 a 不能为字典

    import random
    
    a = "xfs"
    print random.choice(a)
    
    b = [1,2,3]
    print random.choice(b)
    """
    #随机生成学员姓名
    student = [‘张三‘,‘李四‘,‘王五‘,]
    print random.choice(student)
    """
    
    """
    #随机删除列表中一个字符串,直至列表清空
    box = ["a","b","c","d","e","f"]
    for i in range(len(box)):
        ball = random.choice(range(len(box)))
        del box[ball]
        print box
    """
    
    """
    #随机生成手机号
    def phone():
        a = ["136","177","186","131"]
        return random.choice(a)+"".join(random.choice("0123456789")
                                       for i in range(8))
    """
    
    """
    #10内相加计算
    import random
    
    a = random.randint(1,10)
    b = random.randint(1,10)
    
    c = raw_input(str(a) + "+" + str(b) + "=")
    if int(c) == a+b:
        print "答案正确!"
    else:
        print "答案错误!"
    """
    
    """
    #福利彩票生成
    num = ["1","2","3",‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘10‘,‘11‘,‘12‘,‘13‘,‘14‘,‘15‘,‘16‘,
           ‘17‘,‘18‘,‘19‘,‘20‘,‘21‘,‘22‘,‘23‘,‘24‘,‘25‘,‘26‘,‘27‘,‘28‘,‘29‘,
           ‘30‘,‘31‘,‘32‘,‘33‘]
    for i in range(7):
        num1 = num.pop(random.randint(0,len(num)-1))
        if i == 0:
            num2 = num1
        elif i == 1:
            num3 = num1
        elif i == 2:
            num4 = num1
        elif i == 3:
            num5 = num1
        elif i == 4:
            num6 = num1
        elif i == 5:
            num7 = num1
        elif i == 6:
            num8 = num1
    print num2+" "+num3+" "+num4+" "+num5+" "+num6+" "+num7+" "+num8
    """

原文地址:https://www.cnblogs.com/Mr-ZY/p/11761422.html

时间: 2024-08-30 03:40:30

python(time/random模块)的相关文章

Python随机数random模块学习,并实现生成6位验证码

一.前言 学习python随机数random模块的使用 ,并使用模块中的函数,实现6位验证码生成 二.random模块 1.random.random() 返回0-1直接的随机数,类型为float >>>print(random.random()) 0.1259184691662908 2.random.randint(1, 8) 返回1-8直接的随机数,包括8 >>>print(random.randint(1, 8)) 3 3.random.choice() 从一个

python的random模块(生成验证码)

python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) #生成参数1到参数2之间的随机数,输出为int类型,[1,3] random.randrange(1, 3) #生成参数1到参数2之间的随机数,输出为int类型,[1,3),这个方法还有一种用法,就是下面介绍的这种 random.randrange(0,100,2) #从指定范围内,按指定基数递增的集

python之random模块

random模块 用于生成随机浮点数.整数.字符串和随机抽取元素 方法: random()  生成一个随机浮点数,范围在0.0~1.0之间 uniform(上限,下限)  在设置的范围内,随机生成一个浮点数(上下限可以是整数,浮点数) randint(上限,下限)  在设定的范围内,随机生成一个整数(上下限必须为整数) choice(序列)  从任何序列中选取一个随机的元素返回 shuffle(序列)  随机打乱一个序列中元素的顺序 sample(序列,长度)  从指定的序列中随机截取指定长度的

ZH奶酪:【Python】random模块

Python中的random模块用于随机数生成,对几个random模块中的函数进行简单介绍.如下:random.random() 用于生成一个0到1的随机浮点数.如: import random random.random() 输出: 0.3701787746508932 random.uniform(a,b) 用于生成一个指定范围内的随机浮点数,两个参数一个是上线,一个是下线.如: random.uniform(10,20) 输出: 16.878776709127855 random.rand

python 之 random 模块、 shutil 模块、shelve模块、 xml模块

6.12 random 模块 print(random.random()) (0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) [1,3] 大于等于1且小于等于3之间的整数 print(random.randrange(1,3)) [1,3) 大于等于1且小于3之间的整数 print(random.choice ( [1,'23', [4,5] ] ) )   1或者23或者[4,5] print(random.sample( [1,'2

python之-- random模块

random模块random.random():随机打印一个小数random.randint(1,10):随机打印1-10之间的任意数字(包括1和10)random.randrange(1,10):随机打印1-10之间的任意数字(不包括10)random.sample(range(100),5):从100个数字中随机抽取5个数字以列表形式打印.可以用作随机验证码或密码使用 如:random.sample('abcde',3) 随机生成3个字符. 举例:生成随机验证码 第一种写法 1 import

python的random模块函数分析(一)

random是python产生伪随机数的模块,随机种子默认为系统时钟.下面分析模块中的方法: 1.random.randint(start,stop): 这是一个产生整数随机数的函数,参数start代表最小值,参数stop代表最大值,两端的数值都可以取到: 函数算法时间复杂度:O(1)核心源代码:return self.randrange(a, b+1)   # 由randrange函数封装而来例子: 1 for i in range(20): 2 print(rm.randint(0, 10)

Python之 random 模块

#!/usr/bin/env python # -*- coding:utf8 -*- import random ''' 如果想要随机的内容的话,就可以使用这个模块来完成 ''' ############## 随机模块应用函数 ############## def v_code(): ''' 随机验证码函数 ''' ret = "" for i in range(5): num = random.randint(0,9) # 获取随机数字 alf = chr(random.randi

python的random模块

As an example of subclassing, the random module provides the WichmannHill class that implements an alternative generator in pure Python. The class provides a backward compatible way to reproduce results from earlier versions of Python, which used the

python的random模块及加权随机算法的python实现

random是用于生成随机数的,我们可以利用它随机生成数字或者选择字符串. random.seed(x)改变随机数生成器的种子seed. 一般不必特别去设定seed,Python会自动选择seed. random.random()    用于生成一个随机浮点数n,0 <= n < 1 random.uniform(a,b)    用于生成一个指定范围内的随机浮点数,生成的随机整数a<=n<=b; random.randint(a,b)    用于生成一个指定范围内的整数,a为下限,