常用模块(一)

一、时间(time)模块

时间戳 (Timestamp):时间戳表示的是从1970年1月1日00:00:00为计时起点,到当前的时间长度

import time
print(help(time))
查看time模块的官方说明 time.time() # 返回当前时间的时间戳
print(time.time())
>>> 1540191340.5649574 time.clock() # 计算CPU执行的时间
print(time.clock())
>>> 3.6655977783544983e-07 time.sleep() # 延时多少秒
print(time.sleep(3)) time.gmtime() # 结构化时间:将时间戳转换成为标准时间utc时区(0时区)
print(time.gmtime())
>>> time.struct_time(tm_year=2018, tm_mon=10, tm_mday=22, tm_hour=7, tm_min=4, tm_sec=0, tm_wday=0, tm_yday=295, tm_isdst=0) time.localtime() # 本地时间:将一个时间戳转换为当前时区的时间
print(time.localtime())
>>> time.struct_time(tm_year=2018, tm_mon=10, tm_mday=22, tm_hour=15, tm_min=4, tm_sec=26, tm_wday=0, tm_yday=295, tm_isdst=0) time.strftime() # 本地的结构化时间
struct_time = time.localtime()
print(time.strftime('%Y/%m/%d %X',struct_time))
print(time.strftime('%Y/%m/%d %X'))
>>>2018/10/22 21:23:45
2018/10/22 21:23:45 time.strptime() # 提取想要知道的具体时间:把元组转化为格式化的时间字符串。如果t未指定,将传入time.localtime()
print(time.strptime('2018/10/22 09:27:30','%Y/%m/%d %H:%M:%S'))
>>> time.struct_time(tm_year=2018, tm_mon=10, tm_mday=22, tm_hour=9, tm_min=27, tm_sec=30, tm_wday=2, tm_yday=290, tm_isdst=-1)
a = time.strptime('2018/10/22 09:27:30','%Y/%m/%d %H:%M:%S')
print(a.tm_year)
>>> 2018 time.ctime() # 把时间戳转换成为时间,格式为固定的
print(time.ctime())
>>> Mon Oct 22 15:05:04 2018 time.mktime() # 把时间转换成为时间戳
print(time.mktime(time.localtime()))
>>> 1540191919.0

二、datetime模块

import datetime
datetime.datetime.now() # 获取时间
print(datetime.datetime.now())
>>> 2018-10-22 15:05:37.396534 datetime.date.today() # 获取一个日期对象
today = datetime.date.today()
print(today)
>>> 2018-10-22 print(today.ctime())
>>> Mon Oct 22 00:00:00 2018
print(today.timetuple())
>>> time.struct_time(tm_year=2018, tm_mon=10, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=295, tm_isdst=-1)
print(today.toordinal())
>>> 736989
print(datetime.date.fromordinal(today.toordinal()))
>>> 2018-10-22 # 修改日期
date = datetime.date(2018,10,22)
print(date)
date1 = date.replace(year=2018,day=30)
print(date1)
>>> 2018-10-22
2018-10-30

三、random模块

import random
1.random() # 随机生成0-1之间的数字
print(random.random())
>>> 0.034957371535410675 2.randint() # 随机输出范围内的一个整数(包括3)
print(random.randint(1,3))
>>> 3 or 2 or 1 3.randrange() # 随机输出范围内的一个整数(不包括3)
print(random.randrange(1,3))
>>> 1 or 2 choice() # 随机输出一个序列中的一个元素
print(random.choice(['ALEX','MIKE','JOHN','CAT','DOG'])) random.shuffle() #打乱列表的序列,重新排序
list = ['ALEX','MIKE','JOHN']
random.shuffle(list)
print(list)
>>> ['JOHN', 'MIKE', 'ALEX'] sample() # 以列表形式输出一个序列中的随机几个元素
print(random.sample(['ALEX',1,3],1))

Eg.随机生成四位数验证码的两种方法

import random
# 方法1:
def random_code():
code = ''
for i in range(4):
if i == random.randint(0,5):
add_num = random.randrange(10)
else:
add_num = chr(random.randrange(65,91))
code += str(add_num)
print(code)
random_code() # 方法2:
def random_code():
code = ''
for i in range(4):
add = random.choice([random.randrange(10),chr(random.randrange(65,91))])
code += str(add)
print(code)
random_code()

Python学习 :常用模块(一)的更多相关文章

  1. python学习——常用模块

    在学习常用模块时我们应该知道模块和包是什么,关于模块和包会单独写一篇随笔,下面先来了解有关在python中的几个常用模块. 一.什么是模块 常见的场景:一个模块就是一个包含了python定义和声明的文 ...

  2. 三、python学习-常用模块

    一.常用模块 1.math数学模块 在计算机中,所有数值在计算机底层都是约等于机制,并不是精确地 import math #ceil() 向上取整操作 math.ceil(3.1)=>4 #fl ...

  3. python的常用模块之collections模块

    python的常用模块之collections模块 python全栈开发,模块,collections 认识模块 什么是模块?    常见的场景:一个模块就是一个包含了python定义和声明的文件,文 ...

  4. Python学习--Selenium模块

    1. Python学习--Selenium模块介绍(1) 2.Python学习--Selenium模块学习(2) 其他: 1. Python学习--打码平台

  5. Python学习--Selenium模块学习(2)

    Selenium的基本操作 获取浏览器驱动寻找方式 1. 通过手动指定浏览器驱动路径2. 通过 `$PATH`环境变量找寻浏览器驱动 可参考Python学习--Selenium模块简单介绍(1) 控制 ...

  6. Python学习---重点模块的学习【all】

    time     [时间模块] import time # print(help(time)) # time模块的帮助 print(time.time()) # 时间戳 print(time.cloc ...

  7. Python学习——python的常用模块

    模块:用一堆代码实现了某个功能的代码集合,模块是不带 .py 扩展的另外一个 Python 文件的文件名. 一.time & datetime模块 import time import dat ...

  8. python(五)常用模块学习

    版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明. https://blog.csdn.net/fgf00/article/details/52357 ...

  9. Python学习【第7篇】:Python之常用模块2

    hashlib,configparser,logging模块 一.常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希 ...

  10. Python学习【第6篇】:Python之常用模块1

    常用模块一. collocations 模块 时间模块 random模块 os模块 sys模块 序列化模块 re模块 常用模块二:这些模块和面向对象有关 hashlib模块 configparse模块 ...

随机推荐

  1. js 获取URL中参数

    function getQueryString() { var result = location.search.match(new RegExp("[\?\&][^\?\& ...

  2. 二、docker学习笔记——安装redis

    前提:打开powershell(管理员) 1.官网路径 按照官网的做了,但外网无法链接,只好换个做法. 2.docker pull redis 这下载的最新版redis 3.在docker上挂载文件夹 ...

  3. Laravel Query Builder 复杂查询案例:子查询实现分区查询 partition by

    案例 案例:Laravel 在文章列表中附带上前10条评论?,在获取文章列表时同时把每个文章的前10条评论一同查询出来. 这是典型分区查询案例,需要根据 comments 表中的 post_id 字段 ...

  4. IIS 7.5+FCK编辑器+burp suite神器拿webshell

    本人小菜一枚,大牛勿喷 看图: 一个越南狗的网站,看了看好多人来过哦,估计都是在这跪下了,试了好多别人上传滴都不行,看了看是IIS7.5,难怪都卡在这里了,于是小编直接上神器Burp Suite- 截 ...

  5. OC Copy and MutableCopy的使用

    #import <Foundation/Foundation.h> @interface Student : NSObject <NSCopying> // copy代表set ...

  6. 记两个std接口equal_range,set_difference

    1.equal_range equal_range是C++ STL中的一种二分查找的算法,试图在已排序的[first,last)中寻找value,它返回一对迭代器i和j,其中i是在不破坏次序的前提下, ...

  7. AngularJs学习笔记--Scope

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/scope 一.什么是Scope? scope(http://code.angularjs.org/1. ...

  8. Vue-Quill-Editor插件插入图片的改进

    最近在做一个Vue-Clie小项目,使用到了Vue-Quill-Editor这个基于Vue的富文本编辑器插件.这个插件跟Vue契合良好,使用起来比其他的诸如百度UEditor要方便很多,但是存在一个小 ...

  9. 一个关于JSON的异常,获取List对象失败的。。。

    重要的事情放在最前面,,以后不管遇到什么异常都一定要把异常读懂再想办法怎么解决,把异常读懂,异常读懂...... 这个异常我记得以前遇到过,而且好像已经做了笔记,,,,,今天翻了一下竟然没有,,,,, ...

  10. Linux 查看所有登录用户的操作历史

    在linux系统的环境下,不管是root用户还是其它的用户只有登陆系统后用进入操作我们都可以通过命令history来查看历史记录,可是假如一台服务器多人登陆,一天因为某人误操作了删除了重要的数据.这时 ...