time和random模块
# 时间模块
# 三种时间表现形式
# 时间戳
# 格式化的时间字符串
# 元组(struct_time)结构化时间 struct_time元组共有9个元素(年,月,日,时,分,秒,一年中的第几周,一年中的第几天等)
# 索引 # 属性 # 值
# 0 tm_year 2011
# 1 tm_mon 1-12
# 2 tm_mday 1-31
# 3 tm_hour 0-23
# 4 tm_min 0-59
# 5 tm_sec 0-60
# 6 tm_wday 0-6(0表示周一)
# 7 tm_yday 1-366(一年中的第几天)
# 8 tm_isdst 默认为0(是否是夏令时) import time
time.sleep(1) # 延时1s
time.time() # 获取时间戳,以s为单位的浮点数,返回的float类型 # 格式化的时间字符串
print(time.strftime('%Y-%m-%d %H:%M:%S')) # 2018-09-23 13:24:39 # 时间元组结构化时间:locatime()将以个时间戳转换为当前时区的struct_time
print(time.localtime()) # time.struct_time(tm_year=2018, tm_mon=9, tm_mday=23, tm_hour=13, tm_min=32, tm_sec=58, tm_wday=6, tm_yday=266, tm_isdst=0)
# time.struct_time(tm_year=2018, tm_mon=7, tm_mday=24, tm_hour = 13, tm_min=59, tm_sec=37, tm_wday=0)
print(time.localtime().tm_year) # 2018 # 时间戳和结构化时间的转换
t = time.time() # 得到时间戳
print(time.localtime(t)) # time.struct_time(tm_year=2018, tm_mon=9, tm_mday=23, tm_hour=13, tm_min=36, tm_sec=38, tm_wday=6, tm_yday=266, tm_isdst=0)
# gmtime 是从秒得到格林时间的结构化时间
print(time.gmtime(t)) # time.struct_time(tm_year=2018, tm_mon=9, tm_mday=23, tm_hour=5, tm_min=36, tm_sec=38, tm_wday=6, tm_yday=266, tm_isdst=0) # 将结构化时间转换成时间戳时间
print(time.mktime(time.gmtime())) # 1537652433.0 # 格式化时间转换为结构化时间
print(time.strptime('2000-12-31', '%Y-%m-%d')) # time.struct_time(tm_year=2000, tm_mon=12, tm_mday=31, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=366, tm_isdst=-1) # 时间戳转换为格式化时间
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(3000000000))) # 2065-01-24 13:20:00 # random模块 随机数模块
import random # 随机小数
print(random.random()) # 0.22507560552012607 返回大于0且小于1之间的小数
print(random.uniform(1, 3)) # 返回大于1小于3的小数 # 随机整数
print(random.randint(1, 5)) # 大于等于1且小于等于5之间的整数
print(random.randrange(1, 10, 2)) # 大于等于1且小于10之间的奇数 # 随机选择一个返回
print(random.choice([1, '23'])) # 返回1或者23或者 # 随机选择多个返回,返回的个数为函数的第二参数
print(random.sample([1, '23', [4, 5]], 2)) # 列表元素任意2个组合 # [1, '23'] # 打乱列表顺序 item = [1, 3, 5, 7, 9]
random.shuffle(item) # 打乱列表的顺序
print(item) # [3, 9, 5, 1, 7] # 练习:生成随机验证码
time和random模块的更多相关文章
- getpass模块和random模块
getpass模块 用于对密码的隐藏输入案例: import getpass passwd = getpass.getpass("please input your password&quo ...
- Python中的random模块,来自于Capricorn的实验室
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- 随机内容生成(random模块)
摘抄于: 低调的python小子 当梦想照进现实 幸福近在咫尺 [jpg]http://ip.ipwind.cn/msn.png[/jpg] Python中的random模块用于生成随机数.下面介绍 ...
- Python中的random模块
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- Python random模块 例子
最近用到随机数,就查询资料总结了一下Python random模块(获取随机数)常用方法和使用例子. 1.random.random random.random()用于生成一个0到1的随机符点数: ...
- python学习之random模块
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块
正则表达式 语法: mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...
- python random模块 - 小驹的专栏 - 博客频道 - CSDN.NET
python random模块 - 小驹的专栏 - 博客频道 - CSDN.NET python random模块 分类: python 2011-11-15 15:31 6037人阅读 评论(2) ...
- 你真的用好了Python的random模块吗?
random模块 用于生成伪随机数 源码位置: Lib/random.py(看看就好,千万别随便修改) 真正意义上的随机数(或者随机事件)在某次产生过程中是按照实验过程中表现的分布概率随机产生的,其结 ...
- python 简单验证码 random模块
random 模块,产生随机数: chr 将数字转成字母. ascii 数字与字符对应表 链接 import randomtemp=""for i in range(0,4): r ...
随机推荐
- k8s node上查看节点
node执行 mkdir -p /root/.kube master执行 scp admin.conf node1:/root/.kube/config
- lumen-ioc容器测试 (2)
lumen-ioc容器测试 (1) lumen-ioc容器测试 (2) lumen-ioc容器测试 (3) lumen-ioc容器测试 (4) lumen-ioc容器测试 (5) lumen-ioc容 ...
- spring boot:使用poi导出excel电子表格文件(spring boot 2.3.1)
一,什么是poi? 1,poi poi是用来兼容微软文档格式的java api, 它是apache的顶级项目之一, 也是我们在生产环境中导出excel时使用最多的库 2,poi官方网站: http:/ ...
- git stash功能的使用
一,git stash的作用: 能够将所有未提交的修改(工作区和暂存区)保存至堆栈中,用于后续恢复当前工作目录 说明:架构森林是一个专注架构的博客,地址:https://www.cnblogs.com ...
- centos8安装fastdfs6.06集群方式一之:软件下载与安装
一,查看本地centos的版本 [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) 说 ...
- JavaScript实现异步的4中方法
一:背景简介 Javascript语言的执行环境是"单线程"(single thread). 所谓"单线程",就是指一次只能完成一件任务.如果有多个任务,就必须 ...
- bzoj2539 丘比特的烦恼、黑书P333 (最优二分图匹配)
丘比特的烦恼 题目描述 Description 随着社会的不断发展,人与人之间的感情越来越功利化.最近,爱神丘比特发现,爱情也已不再是完全纯洁的了.这使得丘比特很是苦恼,他越来越难找到合适的男女, ...
- 自动识别PC端、移动端,并跳转
PC端和移动端代码是分开的,各有一套代码的情况下: 在PC端的HTML文件head标签中间添加一段自动识别移动端的JavaScript代码: <script type="text/ja ...
- asp.net core的授权过滤器中获取action上的Attribute
var action = context.ActionDescriptor as ControllerActionDescriptor; var permission = action.MethodI ...
- ES7 - 11新特性总结
es7 1 Array.prototype.includes 之前都是使用indexOf判断,没有返回-1,现在includes更加方便 Includes 方法用来检测数组中是否包含某个元素,返回bo ...