原文:https://www.cnblogs.com/yuanchenqi/article/5732581.html

time模块

三种时间表示

在Python中,通常有这几种方式来表示时间:

  • 时间戳(timestamp) :         通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
  • 格式化的时间字符串
  • 元组(struct_time)   :         struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
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 2016 print(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中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行
# 时间,即两次时间差。
import time

#时间戳  #计算
# print(time.time()) #1481321748.481654秒 #结构化时间---当地时间
# print(time.localtime(1531242343))
# t=time.localtime()
# print(t.tm_year)
# print(t.tm_wday)
# #-----#结构化时间---UTC
# print(time.gmtime()) #-----将结构化时间转换成时间戳 # print(time.mktime(time.localtime()))
#------将结构化时间转成字符串时间strftime
#print(time.strftime("%Y---%m-%d %X",time.localtime()))
#------将字符串时间转成结构化时间strptime
#print(time.strptime("2016:12:24:17:50:36","%Y:%m:%d:%X")) # print(time.asctime())
# print(time.ctime()) # import datetime
# print(datetime.datetime.now())

random模块

import random

print(random.random())#(0,1)----float

print(random.randint(1,3))  #[1,3]

print(random.randrange(1,3)) #[1,3)

print(random.choice([1,'',[4,5]]))#

print(random.sample([1,'',[4,5]],2))#[[4, 5], '23']

print(random.uniform(1,3))#1.927109612082716

item=[1,3,5,7,9]
random.shuffle(item)
print(item)
import random

# print(random.random())#0.8882268701490094
# print(random.randint(1,3))#1到3, 2
# print(random.randrange(1,3))#1到2, 2
# print(random.choice([11,22,33,44,55]))#22
# print(random.sample([11,22,33,44,55],2))#[33, 44]
# print(random.uniform(1,4))#1到4的浮点型,3.5961920452336846 ret=[1,2,3,4,5]
random.shuffle(ret)#打乱
print(ret)#[4, 5, 2, 3, 1]
'''
def v_code():
ret=""
for i in range(5):
num=random.randint(0,9)
alf=chr(random.randint(65,122))
s=str(random.choice([num,alf]))
ret+=s
return ret
print(v_code())
'''

验证码:

import random
def v_code():
code = ''
for i in range(5):
num=random.randint(0,9)
alf=chr(random.randint(65,90))
add=random.choice([num,alf])
code += str(add)
return code
print(v_code())

最新文章

  1. 平滑处理Smooth之图像预处理算法-OpenCV应用学习笔记三
  2. 减小Gcc编译程序的体积
  3. 【转】在RedHat上搭建自己Email服务器
  4. Eclipse控制台中文乱码
  5. C++指针初始化总结
  6. 区分IE9/IE8/IE7/IE6及其他浏览器-CSS hack
  7. SoapUI 学习总结-01 环境配置
  8. EF CodeFirst系列(2)---CodeFirst的数据库初始化
  9. idea搜索jar中的类
  10. 基于springboot的SSM框架实现返回easyui-tree所需要数据
  11. kubernetes集群搭建(9):docker 镜像的导入与导出
  12. Java中高级面试必问之多线程TOP50(含答案)
  13. python 爬虫括号的用法
  14. java线上应用问题排查方法和工具
  15. Android查缺补漏(IPC篇)-- 进程间通讯之AIDL详解
  16. java排序。。。
  17. ComicEnhancerPro 系列教程十九:用JpegQuality看JPG文件的压缩参数
  18. mybatis 学习笔记(一):mybatis 初认识
  19. (转)写的非常好的一篇HTTP协议详解
  20. Struts2值栈

热门文章

  1. 去除selet标签默认样式
  2. 2019-1-16-win10-uwp-发布的时候-ILC-编译不通过
  3. python for 循环结构
  4. 阿里云Kubernetes服务上使用Tekton完成应用发布初体验
  5. console.log详细介绍
  6. springboot&mybatis 增删改查系列(一)
  7. css3鼠标移动图片上移效果
  8. jq动画和停止动画
  9. 洛谷P5020 货币系统 题解 模拟
  10. uni-app学习记录04-轮播图和滑屏图片