python第十七天---时间模块、random模块
作完一个作业,开始新的学习:
有由今天的时间有限所有学习了以下两个模块,明天继续!
时间模块、random模块
import time
#!usr/bin/env python
#-*-coding:utf-8-*-
# Author calmyan
import time ,datetime
print(time.process_time())#测量处理器运算时间,不包括sleep时间 print(time.altzone)#返回与UTC时间的时间差 以秒计算
print(time.asctime())#返回时间格式 "Fri May 19 11:12:23 2017"
print(time.time())#时间戳
print(time.localtime())#返回本地时间的struct_time 对象格式
print(time.localtime(time.time()+3600))#返回本地时间的struct_time 对象格式 print(time.gmtime())#返回UTC时间utc时间的struct_time对象格式
print(time.gmtime(time.time()+3600))#返回UTC时间utc时间的struct_time对象格式
print(time.time())#当前时间戳
print(time.asctime(time.localtime()))#返回时间格式 Fri May 19 18:16:42 2017 print(time.ctime())#返回时间格式 Fri May 19 18:16:42 2017 print(time.strptime('2017-05-19 18:16:42','%Y-%m-%d %H:%M:%S' ))#将时间日期字符串转成时间对象 string_2=time.strptime('2017-05-19 18:16:42','%Y-%m-%d %H:%M:%S' )#将时间字符串转成时间对象
stu_2=time.mktime(string_2)#mktime可以将时间对象转为时间戳
print(stu_2) string_3=time.gmtime(stu_2)#转成时间对象
print(string_3)
string_4=time.strftime('%Y-%m-%d %H:%M:%S',string_3)#转成时间字符串
print(string_4) print(datetime.datetime.now())#当前时间,本地2017-05-19 20:16:52.165968
print(datetime.date.fromtimestamp(time.time()) )#时间戳直接转成日期格式 2017-05-19 print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.timedelta(5.3)) #5天时间
print(datetime.timedelta(-6.4)) #-6天时间
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
print(datetime.datetime.now() + datetime.timedelta(seconds=40)) #当前时间+30分 a_time = datetime.datetime.now()
print(a_time.replace(minute=3,hour=2)) #时间替换
print(a_time)
print(a_time.replace(minute=3,hour=2,second=18,day=4)) #时间替换
import random
#随机模块 import random,string#字符模块
a=[1,2,3]
a.append(random.random())#随机小数
a=random.random()#随机小数
print(random.random())
print(random.randint(1,5))#randint 随机整数
print(random.randint(1,8))
print(random.randrange(2,3))#随机整数不包尾数\ print(random.sample(range(100),4))#成在生成器中随机出数字 str_list=string.digits+string.ascii_uppercase+string.ascii_lowercase#将ascii中的字符串赋于变量
print(''.join(random.sample(str_list,4)))#直接用join拼接
print(random.sample(str_list,1))
#str_int=string.a
# for i in range(0,255):
# print('编号:%s %s'%(i,chr(i)))
制作了一个比较low的随机码函数:
def random_str(int=4):#默认为4位
str_int=string.digits#数字0-9
str_upp=string.ascii_uppercase#大写英文
str_low=string.ascii_lowercase#小写字母
str_all=str_int+str_low+str_upp#ascii码常用字符
str_conut=[]
for i in range(int):
if i==0:
str_conut.append(random.sample(str_upp,1)[0])#第一位为大写字母
print(str_conut)
elif i==1:
str_conut.append(random.sample(str_int,1)[0])#第二位为数字
elif i==2:
str_conut.append(chr(random.randint(33,47)))#第三位为特殊字符
elif i==3:
str_conut.append(random.sample(str_low,1)[0])#第四位为小写字母
else:
if len(str_conut)==int:
str_conut=''.join(str_conut)
return str_conut
else:
str_conut.append(random.sample(str_all,1)[0])#其他位为随机字符
str_conut=''.join(str_conut)
return str_conut
python第十七天---时间模块、random模块的更多相关文章
- Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型)
Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型) 一丶软件开发规范 六个目录: #### 对某 ...
- Python模块01/自定义模块/time模块/datetime模块/random模块
Python模块01/自定义模块/time模块/datetime模块/random模块 内容大纲 1.自定义模块 2.time模块 3.datetime模块 4.random模块 1.自定义模块 1. ...
- python常用模块——random模块
参考博客:http://www.360doc.com/content/14/0430/11/16044571_373443266.shtml 今天突然想起python该怎么生成随机数?查了一下,贴出实 ...
- Python:日期和时间的处理模块及相关函数
Python:日期和时间的处理模块及相关函数 Python 提供 time 模块和 calendar 模块用于格式化日期和时间. 一.时间戳 在Python中,时间戳是以秒为单位的浮点小数,它是指格林 ...
- python 全栈开发,Day27(复习, defaultdict,Counter,时间模块,random模块,sys模块)
一.复习 看下面一段代码,假如运行结果有问题,那么就需要在每一步计算时,打印一下结果 b = 1 c = 2 d = 3 a = b+c print(a) e = a + d print(e) 执行输 ...
- Python:time模块/random模块/os模块/sys模块
time 模块 #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取当前时间戳 python中时间日期格式化符号: %y 两位数的 ...
- python常用模块: random模块, time模块, sys模块, os模块, 序列化模块
一. random模块 import random # 任意小数 print(random.random()) # 0到1的任意小数 print(random.uniform(-10, 10)) # ...
- python time模块 sys模块 random模块
1,time模块 python中的内置模块 #1,显示当前时间戳 print(time.time()) #2,字符串格式化 print(time.strftime('%Y-%m-%d-%H-%M-%S ...
- python time模块 sys模块 collections模块 random模块 os模块 序列化 datetime模块
一 collections模块 collections模块在内置的数据类型,比如:int.str.list.dict等基础之上额外提供了几种数据类型. 参考博客 http://www.pythoner ...
随机推荐
- docker学习篇(二)---- 基础篇
引言 在之前的学习中,我知道了docker的三大组件分别是----镜像,容器,仓库.了解了这三个组件也就初步理解了docker.所以我学习了这三个组件,并记录下来. 镜像 docker在运行一个容器时 ...
- Kafka实战解惑
目录 一. kafka简介二. Kafka架构方案三. Kafka安装四. Kafka Client API 4.1 Producers API 4.2 Consumers API 4.3 消息高可靠 ...
- top命令查看进程下线程信息以及jstack的使用
转自:https://www.cnblogs.com/shengulong/p/8513652.html top -Hp pid可以查看某个进程的线程信息 -H 显示线程信息,-p指定pid jsta ...
- springboot+cloud 学习(三)消息中间件 RibbitMQ+Stream
安装RabbitMQ window下安装: (1):下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang.org/dow ...
- API 接口返回值
API 接口返回值 https://blog.csdn.net/baple/article/details/52925772
- 【RabbitMQ】6、rabbitmq生产者的消息确认
通过Publisher Confirms and Returns机制,生产者可以判断消息是否发送到了exchange及queue,而通过消费者确认机制,Rabbitmq可以决定是否重发消息给消费者,以 ...
- hadoop之 hadoop能为企业做什么?
hadoop是什么? Hadoop是一个开源的框架,可编写和运行分不是应用处理大规模数据,是专为离线和大规模数据分析而设计的,并不适合那种对几个记录随机读写的在线事务处理模式.Hadoop=HDFS( ...
- 30个极大提高开发效率的Visual Studio Code插件
译者按: 看完这篇文章,我打算从 Sublime Text 转到 Visual Studio Code 了! 原文: Immensely upgrade your development enviro ...
- Navicat安装及简单使用
一.安装 下载完之后,直接解压出来就能用,看一下解压之后的目录: 双击打开下面这个文件(可以把它添加一个桌面快捷方式,或者添加到任务栏): 然后会提示你输入注册码: 回到navicat的解压出来的文件 ...
- node 跨域问题
node跨域有很多方法 1.引入 中间件cors 我觉的最好的方法 var express=require('express'); var cors=require('cors'); var app= ...