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

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

时间模块、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. Linux入门搭建可视化桌面环境小合集virtual box centOS7.10

    常用命令: 关联主机与虚拟机(方便文件共享): # sudo mount -t vboxsf share(主机文件夹名) /usr/share(虚拟机内自创) Linux shell进入root模式: ...

  2. k8s总结(脑图图片)

  3. Jstl标签汇总

    JSTL的核心标签库标签共13个,使用这些标签能够完成JSP页面的基本功能,减少编码工作. 从功能上可以分为4类:表达式控制标签.流程控制标签.循环标签.URL操作标签.  (1)表达式控制标签:ou ...

  4. SpringBoot集成Mybatis(0配置注解版)

    Mybatis初期使用比较麻烦,需要各种配置文件.实体类.dao层映射关联.还有一大推其它配置.当然Mybatis也发现了这种弊端,初期开发了generator可以根据表结构自动生成实体类.配置文件和 ...

  5. Enumerable转换为DataTable

    今天在项目组公共类库中发现一个 Enumerable类型转换为DataTable,写的挺精简的,拿出来跟大家共享一下. using System; using System.Collections.G ...

  6. window下安装绿色版5.7

    1. 在mysql的安装目录下创建my.ini,并配置必要参数. 2. 执行命令mysqld --initialize-insecure --basedir=/opt/mysql/mysql  --d ...

  7. Go语言程序结构分析初探

    每一种编程语言都有自己的语法.结构以及自己的风格,这也是每种语言展现各自魅力及众不同的地方.Go也不例外,它简单而优雅,与此同时使用起来也很有趣.在本文中,我们将讨论以下几点: Go程序结构 如何运行 ...

  8. 解决SharePoint 2010拒绝访问爬网内容源错误

    今天发现SP爬网出现了问题,持续时间蛮长的,一直爬不到内容. 解决方案: 这里有一条解决在SharePoint 2010搜索爬网时遇到的"拒绝访问错误"的小技巧. 首先要检查默认内 ...

  9. .Net实现微信公众平台开发接口(二) 之 “获取access_token”

    access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token. 接口调用请求说明 http请求方式: GET https://api.weixin.qq.com/ ...

  10. spring boot升级到2.x的坑

    升级到spring boot 2.x后,发现了好多坑,现记录下来. 1.pom文件依赖的变化 1.x中,依赖是这样的: <dependency> <groupId>org.sp ...