本节大纲:

  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.背景 鹰眼功能是WebGIS中的一种常见功能,利用一些开源的框架实现 ...

  2. 解析C#类中的构造函数

    <解析C#类中的构造函数> 一.  C#中的构造函数概述: C#中类包含数据成员和函数成员.函数成员提供了操作类中数据的某些功能,包括方法.属性.构造器和终结器.运算符和索引器. 构造函数 ...

  3. 使用代码为textview设置drawableLeft

    xml中的textView中设置android:drawableLeft: <TextView android:id="@+id/bookTitle" android:lay ...

  4. opengl 读取3ds(stl)文件

    没有解决: mark 几个网站: 1 http://blog.csdn.net/icebergkevin/article/details/18974235 能实现二维的 2 http://www.cn ...

  5. .net提交HTML元素到后台,遇到Request报错 解决方案

    对于.NET MVC 项目来说,在Controller中对应的Action方法上打上标签: [ValidateInput(false)] 在MSDN上:HttpRequest 类使用输入验证标志来跟踪 ...

  6. Asp.Net Core 通过中间件防止图片盗链

    一.原理 要实现防盗链,我们就必须先理解盗链的实现原理,提到防盗链的实现原理就不得不从HTTP协议说起,在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿链接到当前的网页或 ...

  7. 三分钟集成elmah xml 格式日志到mvc站点

    1.通过nuget安装Elmah ELMAH on XML Log 会自动在web.config 文件中添加配置内容,默认不允许远程访问,日志访问路径是 Elmah.axd,不记录500错误 2.修改 ...

  8. python基础之迭代器、装饰器、软件开发目录结构规范

    生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大 ...

  9. spring入门(四)【面向切面编程】

    开发过程中很多时候会用到日志.事务等操作,这些操作如果要写在业务代码中会相当麻烦,这时就会用到面向切面编程(AOP),AOP作为一种编程思想,和OOP有着不同的侧重点,面向对象侧重于万事万物皆对象,而 ...

  10. ahjesus在asp.net中还可以通过设置HttpCookie对象的过期时间为DateTime.MinValue来指定此Cookies为跟随浏览器生效

    ahjesus在asp.net中还可以通过设置HttpCookie对象的过期时间为DateTime.MinValue来指定此Cookies为跟随浏览器生效