Day13 Python基础之time/datetime/random模块一(十一)
time模块
import time
print(help(time))
time.time() #return current time in seconds since the Epoch as a float 时间戳:以秒的形式返回当前时间,从1970年算起
time.clock() #return CPU time since process start as a float 只计算CPU执行的时间
time.sleep() # delay for a number of seconds given as a float
time.gmtime() # convert seconds since Epoch to UTC tuple 结构化时间(UTC英国格林尼治天文台为零时区,北京在东八区,时差为8 个小时)
time.localtime() #convert seconds since Epoch to local time tuple(本地时间)结构化时间
time.strftime() # convert time tuple to string according to format specification 格式化时间
Commonly used format codes:
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
实例:
import time
print(time.strftime('%Y--%m--%d %H:%M:%S '))
输出结果:
2018--06--13 11:52:04 <class 'str'>
strftime(format)
time.strptime((string, format)) #parse string to time tuple according to format specification字符串时间转化成格式化时间
b=time.strptime('2018--06--13 12:02:03','%Y--%m--%d %H:%M:%S')
print(b)
print(b.tm_year)
print(b.tm_min)
输出结果:
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=13, tm_hour=12, tm_min=2, tm_sec=3, tm_wday=2, tm_yday=164, tm_isdst=-1)
2018
2
strptime(string,format)
time.ctime(second) #Convert a time in seconds since the Epoch to a string in local time.把秒的时间转换成格式化本地时间
print(time.ctime(123456))
输出结果:
Fri Jan 2 18:17:36 1970
ctime()
time.mktime() #Convert a time tuple in local time to seconds since the Epoch. #把本地时间转换成秒
print(time.mktime(time.localtime()))
输出结果:
1528863479.0
mktime()
datetime模块
import datetime
1 import datetime
2 print(datetime.datetime.now())
3 输出结果:
4 2018-06-13 12:21:02.344843
datetime
random模块
import random
random.random() #x in the interval [0, 1).
random.randint(1,8) #int x in inteval[1,8]
random.randrange(1,8) #int x in inteval[1,8)
random.choice(sequence) #Choose a random element from a non-empty sequence.
random.sample(sequence,count) #Choose count random elements from a non-empty sequence.
# 验证码函数
import random
def v_code():
code=''
for i in range(5):
add_num=random.randrange(0,9)
add_al=chr(random.randrange(65,91))
# if random.randint(1,2)==1:
# code+=str(add_num)
# else:
# code+=add_al
code+=random.choice([str(add_num),add_al])
print(code) v_code()
随机产生验证码实例
注:可以通过chr把数字转换成相应的字母
Day13 Python基础之time/datetime/random模块一(十一)的更多相关文章
- Python常用模块time & datetime &random 模块
时间模块前言 在Python中,与时间处理有关的模块就包括:time,datetime 一.在Python中,通常有这几种方式来表示时间: 时间戳 格式化的时间字符串 元组(struct_time)共 ...
- Py修行路 python基础 (二十)模块 time模块,random模块,hashlib模块,OS及sys模块
一.前提介绍: 可以开辟作用域的只有类,函数,和模块 for循环 if,else: 不能开辟自己的作用域 避免程序复用和重复调用,将这些写到一个.py文件中,做成一个模块,进行调 ...
- python基础 (序列化,os,sys,random,hashlib)
1.序列化 定义: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然 ...
- python基础之序列化 time random os
序列化与反序列化 json pickle 1.什么是序列化与反序列化? 序列化就是将内存中的数据结构转成一种中间格式储存到硬盘或者基于网络传输 反序列化是网络,硬盘将被序列化的对象重新读到内存 2. ...
- 18 python 初学(time、random 模块)
# _author: lily # _date: 2019/1/13 import time import datetime print(help(time)) # print(time.time() ...
- python学习笔记(七)- 递归、python内置函数、random模块
1.函数的不固定参数: #参数不是必填的.没有限制参数的个数.返回参数组的元组 def syz(*args): #参数组,不限制参数个数 #‘args’参数的名字可以随便命名 print(args) ...
- python(30)——【random模块】【if __name__ =='__main__'】【os模块】
一.random模块(随机模块) 1.random 常用模块介绍 import random print(random.random()) #返回[0,1)之间的随机浮点数 print(random. ...
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- Python基础笔记系列十:模块
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 模块 #1.类比于java中的jar包,模块能让你能够有逻辑地组织你的Py ...
随机推荐
- Linux 忘记登录密码?破解系统登陆密码
1.重启或者开启系统,在如下界面按e 进入救援系统: 2.在linux16 这一行末尾输入:rd.break,以rd.break 的方法重置密码 3.分别执行以下命令 mount -o remount ...
- 【2018.05.05 C与C++基础】C++中的自动废料收集:概念与问题引入
在阅读C++语言的设计与演化一书时,作者多次提到希望能设计出一个自动废料收集,然而出于种种考虑,始终未将自动废料收集纳入标准讨论中,而是由Coder自己考虑是否在程序中实现废料收集. 当然了,许多Ja ...
- css点滴1—八种方式实现元素垂直居中
这里介绍实现元素垂直居中的方式,文章是参考了<css制作水平垂直居中对齐>这一篇文章. 1.行高和高度实现 这种方式实现单行垂直居中是很简单的,但是要保证元素内容是单行的,并且其高度是不变 ...
- HTTP1.0 、1.1
网上有很多资料说明这个,但都很长的,觉得东西太多也记不住,就记点东西,权当笔记. HTTP 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一 ...
- HDFS的dfs.replication不同验证
对于上传文件到hdfs上时,当时hadoop的副本系数是几,这个文件的块数副本数就会有几份,无论以后你怎么更改系统副本系统,这个文件的副本数都不会改变,也就说上传到分布式系统上的文件副本数由当时的系统 ...
- Linux的基础命令
-shutdown –h now(root用户才有效) 立即关机 -shutdown –r now(root用户才有效) 立即重新启动计算机 -reboot ...
- java锁的种类以及辨析(一):自旋锁
作者:山鸡 锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized 和 ReentrantLock等等 ) .这些已经写好提供的锁为我们开发提供了便利,但是锁的具 ...
- centos7下kubernetes(6。运行应用)
Deployment 从一个例子开始 kubectl run nginx-deployment --image=nginx:1.7.9 --replicas=2 kubectl get deploym ...
- JWT(JSON Web Token)
摘要: 在Web应用中,使用JWT替代session并不是个好主意 适合JWT的使用场景 抱歉,当了回标题党.我并不否认JWT的价值,只是它经常被误用. 什么是JWT 根据维基百科的定义,JSON W ...
- [TJOI2017]DNA
嘟嘟嘟 这题怎么想都想不出来,最后还是敲了暴力,喜提40分-- 正解竟然也是暴力-- 用\(s_0\)构造SAM,然后把\(s\)扔上去暴力dfs:记录一个修改次数tot,如果当前不匹配,就tot + ...