time 模块与时间相关的功能的模块

在 Python 中,时间分为三种:

  1、时间戳:是一个时间的表示,根据不同的语言,可以是整数或浮点数,是从1970年1月1日0时0分0秒到现在经历的秒数

  2、UTC时间: 又称为世界协调时间,以英国的格林尼治天文所在地区的时间作为参考的时间,也叫做世界标准时间。中国时间是 UTC+8 东八区

  3、localtime:本地时间,又叫时间元组,是一个包含时间内容的普通元组

import time
print(time.localtime()) # 运行
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=15, tm_hour=14, tm_min=44, tm_sec=34, tm_wday=0, tm_yday=288, tm_isdst=0) # 相应参数
# 索引 内容 属性 值
# 0 年 tm_year 2015
# 1 月 tm_mon 1~12
# 2 日 tm_mday 1~31
# 3 时 tm_hour 0~23
# 4 分 tm_min 0~59
# 5 秒 tm_sec 0~61 60表示闰秒 61保留值
# 6 周几 tm_wday 0~6
# 7 第几天 tm_yday 1~366
# 8 夏令时 tm_isdst 0,1,-1(表示夏令时)

localtime

时间模块的属性

# 获取时间戳  返回浮点型
print(time.time()) # 获取当地时间 返回的是结构化时间
print(time.localtime()) # 时间戳 转结构化
print(time.localtime(time.time())) # 结构化 转时间戳
print(time.mktime(time.localtime())) # 获取UTC时间 返回的还是结构化时间 比中国时间少8小时
print(time.gmtime()) # 将获取的时间转成指定的格式 仅支持结构化时间
print(time.strftime("%Y-%m-%d %H:%M:%S %p", time.localtime())) # 将格式化字符串的时间转为结构化时间 注意: 格式必须匹配
print(time.strptime("2018-10-15 15:00:18", "%Y-%m-%d %H:%M:%S")) # sleep 让当前进程睡眠一段时间 单位是秒
time.sleep(2)
print('over') # 接受时间元组并返回一个可读的形式为"Mon Oct 15 15:09:24 2018"的24个字符的字符串。
print(time.asctime()) # 获取字符串化的当前时间
print(time.ctime())

time

datetime 模块提供日期和时间运算表示的模块

datetime常用属性

import datetime

# 获取当前时间 返回的是格式化字符时间
print(datetime.datetime.now()) # 单独获取某个时间 年 月
d = datetime.datetime.now()
print(d.year)
print(d.day) # 手动指定时间
d2 = datetime.datetime(2018,10,10,10,10,10)
print(d2) # 计算两个时间的差 只能减不能加
print(d - d2) # 替换某个时间
print(d.replace(year=2020)) # 表示时间差的属性 timedelta
print(datetime.timedelta(days=1)) d = datetime.datetime.now()
t1 = datetime.timedelta(days=1)
t2 = datetime.timedelta(weeks=1)
print(t2 - t1)
# 时间差可以和一个datetime进行加减
print(d + t2)

datetime

random 模块:随机数模块

# random() 获取0-1之间的随机小数
# 格式:random.random()
# 返回值:随机0-1之间的小数 print(random.random()) # 运行
0.4549137928915099

random.random()

# choice() 随机返回序列中的某个值
# 格式:random.choice(序列)
# 返回值:序列中的某个值 l = [str(i)+"haha" for i in range(5)]
print(l)
rst = random.choice(l)
print(rst) # 运行
['0haha', '1haha', '2haha', '3haha', '4haha']
3haha

random.choice()

# shuffle() 随机打乱列表
# 格式:random.shuffle(列表)
# 返回值:打乱顺序之后的列表 l1 = [i for i in range(5)]
print(l1) random.shuffle(l1)
print(l1) # 运行
[0, 1, 2, 3, 4]
[2, 3, 0, 4, 1]

random.shuffle()

# randint(a,b): 返回一个a到b之间的随机整数,包含a和b

print(random.randint(0,100))

# 运行
46

random.randint()

# randrange(a,b): 返回一个a到b之间的随机整数,包含a不包含b

print(random.randrange(0,2))

# 运行
0

random.randrange()

# 随机选指定个数
print(random.sample([1,2,3],2)) # 运行
[2, 3]

random.sample()

实现随机验证码,整型和全大写字符,可指定长度

def make_code(i):
res = ""
for j in range(i):
# 随机0到9
num = str(random.randint(0,9))
c = chr(random.randint(65,90))
s = random.choice([num,c])
res += s
return res
print(make_code(4))

随机验证码

Learning-Python【18】:Python常用模块(1)—— time、datetime、randrom的更多相关文章

  1. python常用模块之time&datetime模块

    python常用模块之time&datetime模块 在平常的代码中,我们经常要与时间打交道.在python中,与时间处理有关的模块就包括:time和datetime,下面分别来介绍: 在开始 ...

  2. python笔记之常用模块用法分析

    python笔记之常用模块用法分析 内置模块(不用import就可以直接使用) 常用内置函数 help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像 ...

  3. python基础31[常用模块介绍]

    python基础31[常用模块介绍]   python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的li ...

  4. python基础之常用模块以及格式化输出

    模块简介 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...

  5. Day5 - Python基础5 常用模块学习

    Python 之路 Day5 - 常用模块学习   本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...

  6. Python全栈开发之路 【第六篇】:Python基础之常用模块

    本节内容 模块分类: 好处: 标准库: help("modules") 查看所有python自带模块列表 第三方开源模块: 自定义模块: 模块调用: import module f ...

  7. Python基础之常用模块

    一.time模块 1.时间表达形式: 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: 1.1.时间戳(timestamp) :通常来说,时间 ...

  8. Python基础5 常用模块学习

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

  9. python之路——常用模块

    阅读目录 认识模块 什么是模块 模块的导入和使用 常用模块一 collections模块 时间模块 random模块 os模块 sys模块 序列化模块 re模块 常用模块二 hashlib模块 con ...

  10. Python 五个常用模块资料 os sys time re built-in

    1.os模块   os模块包装了不同操作系统的通用接口,使用户在不同操作系统下,可以使用相同的函数接口,返回相同结构的结果.   os.name:返回当前操作系统名称('posix', 'nt', ' ...

随机推荐

  1. 使用call、apply和bind解决js中烦人的this,事件绑定时的this和传参问题

    1.什么是this 在JavaScript中this可以是全局对象.当前对象或者任意对象,这完全取决于函数的调用方式,this 绑定的对象即函数执行的上下文环境(context). 为了帮助理解,让我 ...

  2. 补充:CSS选择器样式的规范!

    css----页面样式,美化页面 css样式的三个规则 1内联式:直接写在html标签中 <p style="color:red"> 直接对html标签使用 style ...

  3. Python2与python3 文件操作关于打开文件

    #首先在python3中操作文件只有一种选择,那就是open() #而在python2中则有两种方式:file()与open() 两者都能够打开文件,对文件进行操作,也具有相似的用法和参数,但是,这两 ...

  4. [qemu] qemu从源码编译安装

    环境:CentOS7-1804 下载最新的源码: ┬─[tong@T7:~/Src/thirdparty/PACKAGES]─[:: AM] ╰─>$ axel https://download ...

  5. xpinyin模块

    import xpinyin s = xpinyin.Pinyin() #一个实例化,以后了解 print(s.get_pinyin('小小军')) #get_pinyin方法,转出来的拼音,每一个汉 ...

  6. Socket,ServerSocket,WebSocket

    一 区别 首先来说下区别吧, Socket和ServerSocket 指传输层网络接口协议,是基于套接字的服务端和客户端实现. 而WebScoket是应用层协议,是客户端-服务器的异步通信方法,用于双 ...

  7. React时间组件(时分秒补0)页面全局引用

    1.common.js export function formatTime(data){ var d = new Date(data); function doTime(d){ if(d<10 ...

  8. Docker:使用自定义redis.conf运行redis容器(7)

    演示环境:win7+docker toolbox 1.自定义配置文件 首先在Windows环境下准备好配置文件redis 然后打开Quickstart终端输入命令: cp -rf ~/Desktop/ ...

  9. git----------如何创建develop分支和工作流,以及如何将develop上的代码合并到master分支上

    1.点击sourceTree 右上角的git工作流,或弹出一个弹出框,无需修改任何东西直接点击确认就可以创建develop. . 2.这里有两个分支了,当前高亮的就是你当前处在的分支.此时develo ...

  10. MyBatis基础入门《二十》动态SQL(foreach)

    MyBatis基础入门<二十>动态SQL(foreach) 1. 迭代一个集合,通常用于in条件 2. 属性 > item > index > collection : ...