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. 代码管理工具:Git 和 Svn 的简单操作

    1. git 先注册git config --global user.name "name" git config --global user.email "email& ...

  2. Hibernate 5 入门指南-基于Envers

    首先创建\META-INF\persistence.xml配置文件并做简单的配置 <persistence xmlns="http://java.sun.com/xml/ns/pers ...

  3. ubantu下安装pip,python,pycharm,numpy,scipy,matplotlibm,pandas 以及sklearn

    ubuntu 安装 pip 及 pip 常用命令: https://blog.csdn.net/danielpei1222/article/details/62969815 ubuntu下不同版本py ...

  4. LeetCode算法题-Third Maximum Number(Java实现-四种解法)

    这是悦乐书的第222次更新,第235篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第89题(顺位题号是414).给定非空的整数数组,返回此数组中的第三个最大数字.如果不存 ...

  5. Linux 小知识翻译 - 「DNS服务器」

    这次聊聊「DNS服务器」. DNS(Domain Name System)服务器,也被称为域名服务器. 因为使用互联网的时候,必须要指定DNS服务器,所以你一定听过DNS这个名字吧. 那么,这个DNS ...

  6. cf 20190307 Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)

    B. Mike and Children time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. 为什么swift是面向协议的编程--对面向对象机制的改进

    主要目标是提供抽象能力和解决值类型的多态问题 Actually, Abrahams says, those are all attributes of types, and classes are j ...

  8. java的回调

    java的回调 1,明确什么是回调方法 2,采用接口来实现回调 java同步回调 一个类实现了接口,将他传递给另一个类,在另一个类调用接口中方法 public class Bar implements ...

  9. python入门学习:9.文件和异常

    python入门学习:9.文件和异常 关键点:文件.异常 9.1 从文件中读取数据9.2 写入文件9.3 异常9.4 存储数据 9.1 从文件中读取数据 9.1.1 读取整个文件  首先创建一个pi_ ...

  10. Python:Day19 正则表达式补充

    贪婪匹配 贪婪匹配是指字符后面是*+?的时候,都是尽可能多的匹配,如果不想尽可能多的匹配,那么在这三个字符后面加?号即可,这样变成惰性匹配,按最少匹配. ret = re.findall('ab??' ...