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

类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个.py文件组成的代码集合就成为模块。

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

模块分为三种:自定义模块、内置标准模块(又称标准库)、开源模块

time&datetime模块

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Author: Tony Cabel import time,datetime
print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()
# 测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
print(time.altzone/3600) #打印时区
print(time.asctime()) #返回时间格式"Mon Feb 20 15:01:02 2017",
t=time.localtime() #返回本地时间 的struct time对象格式
print(time.localtime(time.time()+3*3600)) #本地时间后三个小时的时间
print(t)
print(t.tm_year,t.tm_yday)
print(time.time()) #时间戳,从1970年一月一日至今的秒数
print(time.gmtime()) #返回UTC时间
print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式,
t2=time.strptime("2017-2-20 11:30","%Y-%m-%d %H:%M") #将 日期字符串 转成 struct时间对象格式
t2_stamp=time.mktime(t2) #将struct时间对象转成时间戳
print(t2)
t3=time.localtime(t2_stamp) #将本地时间戳转换成struct_time格式
t3_str=time.strftime("%Y-%m-%d-%H-%M.log",t3) #将utc struct_time格式转成指定的字符串格式
print(t3_str)
t4=datetime.datetime.now() #返回当前时间,格式:2017-02-20 15:01:02.487076
print(t4)
t4_fr=t4-datetime.timedelta(days=3) #返回三天前的这个时候的时间,也可以小时分钟秒
print(t4_fr)
print(t4.replace(year=2016,month=3,hour=12)) #将当前时间进行任意替换

时间转换路径:

 
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模块

import random
print(random.random()) #打印一个随机小数,小数点后十六位
print(random.randint(1,3)) #随机打印一个从1到3的数,包括3
print(random.randrange(1,10)) #在range范围内打印一个随机数 import string
src=string.ascii_letters+string.digits #生成随机密码的一种方式
print(''.join(random.sample(src,6))) checkcode='' #另一种生成随机验证码方式
for i in range(6):
current=random.randint(0,6)
if current != i:
temp=chr(random.randint(65,90))
else:
temp=random.randint(0,9)
checkcode+=str(temp)
print(checkcode)

备注:用random.sample不会生成重复的字符,也就是说第一种方式生成的验证码中不会有重复的字符,而第二种方式生成的可能会有重复的字符。

python时间模块和random模块的更多相关文章

  1. python第十七天---时间模块、random模块

    作完一个作业,开始新的学习: 有由今天的时间有限所有学习了以下两个模块,明天继续! 时间模块.random模块 import time #!usr/bin/env python #-*-coding: ...

  2. Python基础系列讲解——random模块随机数的生成

    随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等.Python内置的random模块提供了生成随机数的方法,使用这些方法时需要导入ran ...

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

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

  4. (转)python常用模块(模块和包的解释,time模块,sys模块,random模块,os模块,json和pickle序列化模块)

    阅读目录 1.1.1导入模块 1.1.2__name__ 1.1模块 什么是模块: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代 ...

  5. [时间模块、random模块]

    [时间模块.random模块] time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏 ...

  6. Python之数据加密与解密及相关操作(hashlib模块、hmac模块、random模块、base64模块、pycrypto模块)

    本文内容 数据加密概述 Python中实现数据加密的模块简介 hashlib与hmac模块介绍 random与secrets模块介绍 base64模块介绍 pycrypto模块介绍 总结 参考文档 提 ...

  7. python常用模块之random模块

    python常用模块之random模块 在程序中很多会用到随机字符,比如登陆网站的随机验证码,通过random模块可以很容易生成随机字符串 1.random.randrange():返回1-10之间的 ...

  8. Python 入门之 内置模块 -- random模块

    Python 入门之 内置模块 -- random模块 1.random模块 import random # random -- 随机数 (1)选择1-50之间随机的整数 print(random.r ...

  9. Python之时间模块、random模块、json与pickle模块

    一.时间模块 1.常用时间模块 import time # 时间分为三种格式 #1.时间戳---------------------以秒计算 # start= time.time() # time.s ...

  10. python学习之老男孩python全栈第九期_day019知识点总结——collections模块、时间模块、random模块、os模块、sys模块

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

随机推荐

  1. Scrum 冲刺博客第四篇

    一.当天站立式会议照片一张 二.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中 昨天已完成的工作 新建立了一个list页面用来显示题目,并且创建了列表用来进行题目的存放,将 ...

  2. vmware8~12最新版本 克隆Centos6.X 系列虚拟机网卡无法启动问题 (三步即可)

    1.因工作或者学习需要,都需要在VM上克隆一台服务器,此时无论是快捷克隆(相当于快照的机体)或者完整克隆,都会碰到IP问题. 如:创建后症状:启动之后使用ifconfig,发现无ip地址,只有回环地址 ...

  3. Simotion 监控问题:Could not add self-signed certificate to certificate store.

    使用OPC UA 连接设备,在创建客户端证书时总是报这个错误:Could not add self-signed certificate to certificate store.. 解决方法,用管理 ...

  4. WPF的MediaElement指定Source无法播放问题解决

    最近学wpf,在使用 MediaElement 指定 Source 进行视频播放时,在源码界面可以正常显示,但运行时控件显示空白. 源码界面如下图:(可正常显示) 运行后如下图所示:(控件位置显示空白 ...

  5. Spring与SpringMVC的关系

    在此鉴于你已经了解过Spring的相关知识,简单描述一下Spring与Spring的关系 在框架的使用中,Spring类似于一个具有多种特性,也可以说是多种功能模块的应用平台,(特性就比如IoC,AO ...

  6. kinect 深度图像去噪算法

    算法设计思路 (1)读取16位深度图像到待处理图像帧组: (2)ROI区域计算 由于kinect 彩色摄像头和红外深度摄像头是存在视角偏差的,经过视角对齐后,得到的深度图像是有黑边的.此处通过取帧组第 ...

  7. Spring入门(三)— AOP注解、jdbc模板、事务

    一.AOP注解开发 导入jar包 aop联盟包. aspectJ实现包 . spring-aop-xxx.jar . spring-aspect-xxx.jar 导入约束 aop约束 托管扩展类和被扩 ...

  8. Laravel之Ueditor

    1.访问网址http://ueditor.baidu.com/website/download.html下载合适的编辑器版本 2.按照插件包中的index.html样式,布局页面 3.如果需要使用表单 ...

  9. JS之 if语句函数 对接事件动作 函数更改css css对接需要换妆的区id或class

      if 函数的实现步骤: function +名字() 指定id , 指定开关(display: none or block) if + else 构成逻辑 控制开关 决定在哪里安置一个灯泡, 指定 ...

  10. Flume -- Transfer one type of source to another type

    Source within Flume is a kind of Server for outside client. Sink within Flume is a kind of client fo ...