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模块一(十一)的更多相关文章

  1. Python常用模块time & datetime &random 模块

    时间模块前言 在Python中,与时间处理有关的模块就包括:time,datetime 一.在Python中,通常有这几种方式来表示时间: 时间戳 格式化的时间字符串 元组(struct_time)共 ...

  2. Py修行路 python基础 (二十)模块 time模块,random模块,hashlib模块,OS及sys模块

    一.前提介绍: 可以开辟作用域的只有类,函数,和模块            for循环 if,else: 不能开辟自己的作用域 避免程序复用和重复调用,将这些写到一个.py文件中,做成一个模块,进行调 ...

  3. python基础 (序列化,os,sys,random,hashlib)

    1.序列化 定义: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然 ...

  4. python基础之序列化 time random os

    序列化与反序列化 json  pickle 1.什么是序列化与反序列化? 序列化就是将内存中的数据结构转成一种中间格式储存到硬盘或者基于网络传输 反序列化是网络,硬盘将被序列化的对象重新读到内存 2. ...

  5. 18 python 初学(time、random 模块)

    # _author: lily # _date: 2019/1/13 import time import datetime print(help(time)) # print(time.time() ...

  6. python学习笔记(七)- 递归、python内置函数、random模块

    1.函数的不固定参数: #参数不是必填的.没有限制参数的个数.返回参数组的元组 def syz(*args): #参数组,不限制参数个数 #‘args’参数的名字可以随便命名 print(args) ...

  7. python(30)——【random模块】【if __name__ =='__main__'】【os模块】

    一.random模块(随机模块) 1.random 常用模块介绍 import random print(random.random()) #返回[0,1)之间的随机浮点数 print(random. ...

  8. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  9. Python基础笔记系列十:模块

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 模块 #1.类比于java中的jar包,模块能让你能够有逻辑地组织你的Py ...

随机推荐

  1. 关于C#List中FindAll用法的一些简单示例

    using System; using System.Collections.Generic; public partial class List : System.Web.UI.Page { pro ...

  2. 【MM系列】SAP 根据PO查找对应的打印FORM

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 根据PO查找对应的打印FOR ...

  3. 三、Tableau筛选器的使用

    一.使用筛选器制作联动效果 注意,下面两幅图以‘是否盈利’来添加颜色标签 图一:地图 图二:月度销售客户细分 图三:月度销售产品类别 1.新增仪表盘:将图一.图二.图三拉进同一个仪表盘 2.在图一原图 ...

  4. org.hibernate.NonUniqueObjectException

    错误如下: 2017-3-29 15:17:52~ERROR~org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV ...

  5. c/c++ 重载运算符的思考

    c/c++ 重载运算符的思考 #include <iostream> using namespace std; class Imaginary{ public: Imaginary():r ...

  6. Windows Server 2016-Powershell管理站点复制

    对于Active Directory的Windows PowerShell包括管理复制.网站.域和森林,域控制器以及分区的能力.例如Active Directory的站点和服务管理单元和repadmi ...

  7. 利用fpm制作rpm包

    使用fpm制作rpm包 安装如下 [root@web01 ~]# yum install -y gcc zlib zlib-devel wget http://ruby.taobao.org/mirr ...

  8. MVC+EF 序列化类型为“System.Data.Entity.DynamicProxies.__的对象时检测到循环引用

    用MVC+EF做简单查询时,返回json格式数据出现问题 原代码: public ActionResult JSon({ NorthwindEntities db = new NorthwindEnt ...

  9. IIS 发布ASP.NET MVC 4.0 错误500.21解决办法

    由VS2013 写好的MVC 4.0 发布在服务器IIS 上报错500.21,解决办法:尝试多种网上介绍的办法,最后发现还是模块问题.

  10. Spring的AOP基于AspectJ的注解方式开发2

    参考自黑马培训机构 上一篇博客提到了在配置文件中开启aop的注解开发,以及简单使用了@Before,@Aspect 这是为了告诉spring为前置通知和切面类 接下来介绍aop的注解的通知类型,和切入 ...