本节大纲:

  1. 模块介绍
  2. time &datetime模块
  3. random

一、模块介绍:

模块,用一砣代码实现了某个功能的代码集合。

类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成

(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。

如:os 是系统相关的模块;file是文件操作相关的模块

模块分为三种:

①自定义模块

②内置标准模块(又称标准库)

③开源模块

二、time &datetime模块

 1 import time
 2 import datetime
 3
 4 print(time.clock()) #返回处理器时间,3.3开始已废弃
 5 print(time.process_time()) #返回处理器时间,3.3开始已废弃
 6 print(time.time()) #返回当前系统时间戳
 7 print(time.ctime()) #输出Tue Jan 26 18:23:48 2016 ,当前系统时间
 8 print(time.ctime(time.time()-86640)) #将时间戳转为字符串格式
 9 print(time.gmtime(time.time()-86640)) #将时间戳转换成struct_time格式
10 print(time.localtime(time.time()-86640)) #将时间戳转换成struct_time格式,但返回 的本地时间
11 print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time格式转回成时间戳格式
12 #time.sleep(4) #sleep
13 print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将struct_time格式转成指定的字符串格式
14 print(time.strptime("2016-01-28","%Y-%m-%d") ) #将字符串格式转换成struct_time格式
15
16 #datetime module
17
18 print(datetime.date.today()) #输出格式 2016-01-26
19 print(datetime.date.fromtimestamp(time.time()-864400) ) #2016-01-16 将时间戳转成日期格式
20 current_time = datetime.datetime.now() #
21 print(current_time) #输出2016-01-26 19:04:30.335935
22 print(current_time.timetuple()) #返回struct_time格式
23
24 #datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
25 print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换
26
27 str_to_date = datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") #将字符串转换成日期格式
28 new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
29 new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
30 new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
31 new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s
32 print(new_date)

格式如下表格:

Directive Meaning Notes
%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.  
%d Day of the month as a decimal number [01,31].  
%H Hour (24-hour clock) as a decimal number [00,23].  
%I Hour (12-hour clock) as a decimal number [01,12].  
%j Day of the year as a decimal number [001,366].  
%m Month as a decimal number [01,12].  
%M Minute as a decimal number [00,59].  
%p Locale’s equivalent of either AM or PM. (1)
%S Second as a decimal number [00,61]. (2)
%U

Week number of the year (Sunday as the first day of the week) as a decimal number [00,53].

All days in a new year preceding the first Sunday are considered to be in week 0.

(3)
%w Weekday as a decimal number [0(Sunday),6].  
%W

Week number of the year (Monday as the first day of the week) as a decimal number [00,53].

All days in a new year preceding the first Monday are considered to be in week 0.

(3)
%x Locale’s appropriate date representation.  
%X Locale’s appropriate time representation.  
%y Year without century as a decimal number [00,99].  
%Y Year with century as a decimal number.  
%z

Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM,

where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].

 
%Z Time zone name (no characters if no time zone exists).  
%% A literal '%' character.

二、random模块

1、随机数

eg:

 import random
 print random.random()
 print random.randint(1,2)
 print random.randrange(1,10)

2、生成随机验证码

eg:

 import random
 checkcode = ''
 for i in range(4):
     current = random.randrange(0,4)
     if current != i:
         temp = chr(random.randint(65,90))
     else:
         temp = random.randint(0,9)
     checkcode += str(temp)
 print checkcode

Python 常用模块之time&datetime 和random的更多相关文章

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

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

  2. Python——常用模块(time/datetime, random, os, shutil, json/pickcle, collections, hashlib/hmac, contextlib)

    1.time/datetime 这两个模块是与时间相关的模块,Python中通常用三种方式表示时间: #时间戳(timestamp):表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. ...

  3. Python常用模块(logging&re&时间&random&os&sys&shutil&序列化&configparser&&hashlib)

    一. logging(日志模块) 二 .re模块 三. 时间模块 四. random模块 五. os模块 六. sys模块 七. shutil模块 八. 序列化模块(json&pickle&a ...

  4. Python常用标准库之datetime、random、hashlib、itertools

    库:具有相关功能模块的集合 import sys.builtin_module_names #返回内建模块的名字modules 查看所有可用模块的名字 1.1.1获取当前日期和时间 from date ...

  5. Python 常用模块系列学习(1)--random模块常用function总结--简单应用--验证码生成

    random模块--random是一个生成器 首先: import random    #导入模块 print (help(random))    #打印random模块帮助信息 常用function ...

  6. python常用模块---collections、time、random、os、sys、序列号模块

    collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict. ...

  7. python 常用模块之random,os,sys 模块

    python 常用模块random,os,sys 模块 python全栈开发OS模块,Random模块,sys模块 OS模块 os模块是与操作系统交互的一个接口,常见的函数以及用法见一下代码: #OS ...

  8. Python常用模块-随机数模块(random)

    Python常用模块-随机数模块(random) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常用方法举例 #!/usr/bin/env python #_*_coding: ...

  9. Python常用模块-时间模块(time&datetime)

    Python常用模块-时间模块(time & datetime) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.初始time模块 #!/usr/bin/env pyth ...

随机推荐

  1. (十八)WebGIS中清空功能和地图定位功能的设计以及实现

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 当地图中增加了很多元素后,对不同的元素需要进行一定的控制,最 ...

  2. Android置底一个View后运行报错

    大致问题是 放一个LinearLayout ID @+id/layout ,然后在它上面放一个button 设置android:layout_above="@id/layout" ...

  3. MongoDB-基础-limit-skip-sort

    MongoDB Limit() 方法 如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的 ...

  4. UML类图简单介绍

    先上一张图: 概述 类图(Class Diagram)是面向对象系统建模中最常用和最重要的图,是定义其它图的基础.类图主要是用来显示系统中的类.接口以及它们之间的静态结构和关系的一种静态模型. 类图组 ...

  5. SQL语句分组排序,多表关联排序

    SQL语句分组排序,多表关联排序总结几种常见的方法: 案例一: 在查询结果中按人数降序排列,若人数相同,则按课程号升序排列? 分析:单个表内的多个字段排序,一般可以直接用逗号分割实现. select ...

  6. [WCF编程]12.事务:服务事务编程(上)

    一.设置环境事务 默认情况下,服务类和操作没有环境事务,即使客户端事务传播到服务端也是如此. 尽管强制事务流从客户端传播过来,但服务端的环境事务依旧为null.为了启用环境事务,每个操作必须告诉WCF ...

  7. sql语句优化SQL Server

    MS   SQL   Server查询优化方法查询速度慢的原因很多,常见如下几种 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)          2.I/O吞吐量小,形成了 ...

  8. 纪念品分组 2007年NOIP全国联赛普及组

    题目描述 元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作.为使得参加晚会的同学所获得的纪念品价值相对均衡,他要把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品,并且每组纪念品的价格 ...

  9. 连接输出 如果存在在php中多次echo输出js的时候

  10. 转载:《TypeScript 中文入门教程》 10、混入

    版权 文章转载自:https://github.com/zhongsp 建议您直接跳转到上面的网址查看最新版本. 介绍 除了传统的面向对象继承方式,还流行一种通过可重用组件创建类的方式,就是联合另一个 ...