Python time & random模块
time模块
三种时间表示
在Python中,通常有这几种方式来表示时间:
- 时间戳(timestamp) : 通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
- 格式化的时间字符串
- 元组(struct_time) : struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
|
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
import time# 1 time() :返回当前时间的时间戳time.time() #1473525444.037215#----------------------------------------------------------# 2 localtime([secs])# 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。time.localtime() #time.struct_time(tm_year=2016, tm_mon=9, tm_mday=11, tm_hour=0,# tm_min=38, tm_sec=39, tm_wday=6, tm_yday=255, tm_isdst=0)time.localtime(1473525444.037215)#----------------------------------------------------------# 3 gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。#----------------------------------------------------------# 4 mktime(t) : 将一个struct_time转化为时间戳。print(time.mktime(time.localtime()))#1473525749.0#----------------------------------------------------------# 5 asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。# 如果没有参数,将会将time.localtime()作为参数传入。print(time.asctime())#Sun Sep 11 00:43:43 2016#----------------------------------------------------------# 6 ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为# None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。print(time.ctime()) # Sun Sep 11 00:46:38 2016print(time.ctime(time.time())) # Sun Sep 11 00:46:38 2016# 7 strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和# time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个# 元素越界,ValueError的错误将会被抛出。print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56# 8 time.strptime(string[, format])# 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X'))#time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,# tm_wday=3, tm_yday=125, tm_isdst=-1)#在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。# 9 sleep(secs)# 线程推迟指定的时间运行,单位为秒。# 10 clock()# 这个需要注意,在不同的系统上含义不同。在UNIX系统上,它返回的是“进程时间”,它是用秒表示的浮点数(时间戳)。# 而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行# 时间,即两次时间差。 |


|
1
2
|
help(time)help(time.asctime) |
random模块
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import randomprint(random.random())#(0,1)----floatprint(random.randint(1,3)) #[1,3]print(random.randrange(1,3)) #[1,3)print(random.choice([1,'23',[4,5]]))#23print(random.sample([1,'23',[4,5]],2))#[[4, 5], '23']print(random.uniform(1,3))#1.927109612082716item=[1,3,5,7,9]random.shuffle(item)print(item) |
验证码
Python time & random模块的更多相关文章
- 【转】python之random模块分析(一)
[转]python之random模块分析(一) random是python产生伪随机数的模块,随机种子默认为系统时钟.下面分析模块中的方法: 1.random.randint(start,stop): ...
- python的random模块(生成验证码)
python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) # ...
- Python中random模块生成随机数详解
Python中random模块生成随机数详解 本文给大家汇总了一下在Python中random模块中最常用的生成随机数的方法,有需要的小伙伴可以参考下 Python中的random模块用于生成随机数. ...
- 你真的用好了Python的random模块吗?
random模块 用于生成伪随机数 源码位置: Lib/random.py(看看就好,千万别随便修改) 真正意义上的随机数(或者随机事件)在某次产生过程中是按照实验过程中表现的分布概率随机产生的,其结 ...
- Python之random模块
random模块 产生随机数的模块 是Python的标准模块,直接导入即可 import random 1)随机取一个整数,使用.randint()方法: import random print(ra ...
- Python:random模块
近排练习代码时候经常会用到random模块,以防后面忘记还是需要记录一下. 首先导入模块: import random random.random():用于生成一个0到1的随机浮点数: 0 <= ...
- ZH奶酪:【Python】random模块
Python中的random模块用于随机数生成,对几个random模块中的函数进行简单介绍.如下:random.random() 用于生成一个0到1的随机浮点数.如: import random ra ...
- python 之 random 模块、 shutil 模块、shelve模块、 xml模块
6.12 random 模块 print(random.random()) (0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) [1,3] 大 ...
- Python 之 random模块
Python中的random模块用于生成随机数.1.random.random() #用于生成一个0到1的随机浮点数:0<= n < 1.0>>> random.ran ...
随机推荐
- 逆向工程之App脱壳
http://www.cnblogs.com/ludashi/p/5725743.html iOS逆向工程之App脱壳 本篇博客以微信为例,给微信脱壳."砸壳"在iOS逆向工程中是 ...
- Linux I2C设备驱动编写(一)【转】
本文转载自:http://blog.csdn.net/airk000/article/details/21345457 在Linux驱动中I2C系统中主要包含以下几个成员: I2C adapter 即 ...
- 第二章 在Html中使用JavaScript
https://www.jianshu.com/p/8247a9401725 2.1 Script元素 https://developer.mozilla.org/en-US/docs/Web/HTM ...
- sql server的版本检查
https://support.microsoft.com/en-ph/help/321185/how-to-determine-the-version-edition-and-update-leve ...
- ubuntu安装go语言
1.下载安装包 2.解压 sudo tar -zvxf go1.10.linux-amd64.tar.gz -C /usr/local 3.配置 sudo vim /etc/profile 添加 #s ...
- 不仅开源,而且对企业应用完全免费!ExtAspNet弃用GPL v2,拥抱Apache License 2.0(转)
不仅开源,而且对企业应用完全免费!ExtAspNet弃用GPL v2,拥抱Apache License 2.0(转) 提出问题 ExtAspNet开源以来,一直坚持开源免费的原则,但是其GPL v2的 ...
- Kernel trick----PRML读书笔记
Many linear parametric models can be re-cast into an equivalent 'dual representstion' in which the p ...
- Django day28 频率组件,解析器
一:频率组件: 1.频率是什么? 节流,访问控制 2. (1)内置的访问频率控制类SimpleRateThrottle (2)写一个类,继承SimpleRateThrottle class MyThr ...
- url参数为数组
//url中state参数为数组 ?baseline_id=12&version_id=34&state[]=complete&state[]=hangup&state ...
- 51nod1446 Kirchhoff矩阵+Gauss消元+容斥+折半DFS
思路: //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> using ...