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 ...
随机推荐
- Cookie的存储、获取、删除操作
var Cookie={ set: function (name, value, days) { var d = new Date; d.setTime(d.getTime() + 24*60*60* ...
- 对nginx中location的认识
关于一些对location认识的误区 1.location的匹配顺序是“先匹配正则,在匹配普通”. location的匹配顺序其实是“先匹配普通,在匹配正则”.造成误解的原因是:正则匹配会覆盖普通匹配 ...
- 异步与并行~CancellationTokenSource对线程的作用
返回目录 说起CancellationTokenSource我们应该不会陌生,对于Thread,Task来说,我们启动一个线程去做一些事,如果希望它在某个阶段去被动的停止,可以使用这个Cancella ...
- Spring Cloud Ribbon入门
一.简介 Spring Cloud Ribbon是一个基于Http和TCP的客户端负载均衡工具,它是基于Netflix Ribbon实现的.它不像服务注册中心.配置中心.API网关那样独立部署,但是它 ...
- 一文揭秘定时任务调度框架quartz
之前写过quartz或者引用过quartz的一些文章,有很多人给我发消息问quartz的相关问题, quartz 报错:java.lang.classNotFoundException quartz源 ...
- 【CSS学习】--- z-index属性
一.前言 网页显示实际上是三维的,我们直观看到的有x轴和y轴,但在网页布局上还有一个z轴. 对于定位元素,我们使用top.right.left.bottom来实现元素在x-y平面上的定位,但为了表示布 ...
- SpringMVC教程4
SpringMVC教程3 一.数据回写 数据回写:在做数据更新的时候服务端查询的数据自动填充到表单中. 1.1默认方式 通过前面讲解的 Map Mode ModelMap绑定数据 @RequestMa ...
- K均值聚类
聚类(cluster)与分类的不同之处在于, 分类算法训练过程中样本所属的分类是已知的属监督学习. 而聚类算法不需要带有分类的训练数据,而是根据样本特征的相似性将其分为几类,又称为无监督分类. K均值 ...
- List排序Collections.sort 重写compare
static List<Integer> intList = Arrays.asList(2,5,7, 3, 1); public static void main(String[] ar ...
- canvas-star5.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...