作完一个作业,开始新的学习:

有由今天的时间有限所有学习了以下两个模块,明天继续!

时间模块、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模块的更多相关文章

  1. Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型)

    Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型) 一丶软件开发规范 六个目录: #### 对某 ...

  2. Python模块01/自定义模块/time模块/datetime模块/random模块

    Python模块01/自定义模块/time模块/datetime模块/random模块 内容大纲 1.自定义模块 2.time模块 3.datetime模块 4.random模块 1.自定义模块 1. ...

  3. python常用模块——random模块

    参考博客:http://www.360doc.com/content/14/0430/11/16044571_373443266.shtml 今天突然想起python该怎么生成随机数?查了一下,贴出实 ...

  4. Python:日期和时间的处理模块及相关函数

    Python:日期和时间的处理模块及相关函数 Python 提供 time 模块和 calendar 模块用于格式化日期和时间. 一.时间戳 在Python中,时间戳是以秒为单位的浮点小数,它是指格林 ...

  5. python 全栈开发,Day27(复习, defaultdict,Counter,时间模块,random模块,sys模块)

    一.复习 看下面一段代码,假如运行结果有问题,那么就需要在每一步计算时,打印一下结果 b = 1 c = 2 d = 3 a = b+c print(a) e = a + d print(e) 执行输 ...

  6. Python:time模块/random模块/os模块/sys模块

    time 模块 #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取当前时间戳 python中时间日期格式化符号: %y 两位数的 ...

  7. python常用模块: random模块, time模块, sys模块, os模块, 序列化模块

    一. random模块  import random # 任意小数 print(random.random()) # 0到1的任意小数 print(random.uniform(-10, 10)) # ...

  8. python time模块 sys模块 random模块

    1,time模块 python中的内置模块 #1,显示当前时间戳 print(time.time()) #2,字符串格式化 print(time.strftime('%Y-%m-%d-%H-%M-%S ...

  9. python time模块 sys模块 collections模块 random模块 os模块 序列化 datetime模块

    一 collections模块 collections模块在内置的数据类型,比如:int.str.list.dict等基础之上额外提供了几种数据类型. 参考博客 http://www.pythoner ...

随机推荐

  1. iptables防DDOS攻击和CC攻击设置

    防范DDOS攻击脚本 #防止SYN攻击 轻量级预防 iptables -N syn-flood iptables -A INPUT -p tcp --syn -j syn-flood iptables ...

  2. Redis学习系列七分布式锁

    一.简介 熟悉.Net多线程的都知道,当多个线程同时操作一个全局缓存对象(static对象实例.Dictionary.List等)时,会存在多线程争用问题,包括EF.Dapper等本身的缓存机制,都存 ...

  3. 使用Project进行项目管理

    下面开始介绍Project的使用. 1.  从下列地址获取Project 2010的副本. 版权问题,已删除地址. 2.安装 2.1 版权页 2.2 自定义安装页 2.3 安装完毕. 3.使用该软件进 ...

  4. vue-05-webpack安装-vue单文件启动

    1, webpack是什么 1), 是一个打包工具, 比gulp, grunt更先进 2), 额外功能 项目部署上线, 清空目录等 hot module reload, 页面刷新后, 数据不变化 3) ...

  5. 自己动手实现java数据结构(三) 栈

    1.栈的介绍 在许多算法设计中都需要一种"先进后出(First Input Last Output)"的数据结构,因而一种被称为"栈"的数据结构被抽象了出来. ...

  6. 方便操作的命名范围scope

    <?php namespace Goods\Model; use Think\Model; class GoodsModel extends Model { protected $_scope ...

  7. 页面滚动显示或隐藏元素Headroom.js插件帮助你实现滚动效果

    Headroom.js 是什么? Headroom.js 是一个轻量级.高性能的JS小工具(不依赖任何工具库!),它能在页面滚动时做出响应.此页面顶部的导航条就是一个鲜活的案例,当页面向下滚动时,导航 ...

  8. .Net Core Cors中间件解析

    一.同源策略和资源跨域共享 1.同源策略 同源策略,它是由Netscape提出的一个著名的安全策略.现在所有支持JavaScript 的浏览器都会使用这个策略.所谓同源是指,域名,协议,端口相同. 1 ...

  9. FFmpeg内存IO模式(内存区作输入或输出)

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10318145.html 所谓内存IO,在FFmpeg中叫作"buffered ...

  10. 数据分析之pandas模块

    一.Series 类似于一位数组的对象,第一个参数为数据,第二个参数为索引(索引可以不指定,就默认用隐式索引) Series(data=np.random.randint(1,50,(10,))) S ...