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

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

时间模块、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. [EXP]XAMPP 5.6.8 - SQL Injection / Persistent Cross-Site Scripting

    <!-- # Exploit Title: SQL injection (and previous) # Date: -- # Exploit Author: Rafael Pedrero # ...

  2. Spring Data JPA例子[基于Spring Boot、Mysql]

    关于Spring Data Spring社区的一个顶级工程,主要用于简化数据(关系型&非关系型)访问,如果我们使用Spring Data来开发程序的话,那么可以省去很多低级别的数据访问操作,如 ...

  3. 基于redis的分布式ID生成器

    基于redis的分布式ID生成器  

  4. redis.properties

    #### env:${env} redis.maxIdle= ##最小空闲数 redis.minIdle= ##最大连接数:能够同时建立的“最大链接个数” redis.maxTotal= #每次最大连 ...

  5. Spring Framework简介

    作者关于此主题早期文章 Spring框架快速入门 起源 要谈Spring的历史,就要先谈J2EE.J2EE应用程序的广泛实现是在1999年和2000年开始的,它的出现带来了诸如事务管理之类的核心中间层 ...

  6. Java和Python分别实现直接选择排序

    1.基本思想 将指定排序位置与其他数组元素分别对比,如果满足条件就进行交换.个人理解其实就是每趟循环从数组里选一个最大的值(最小的值)放到数组最后(最前). 2.算法实现 这里以每趟循环从数组中选择一 ...

  7. python字典操作和内置方法

    一 字典基本介绍 python中只有字典是映射结构,通过key取值,并且key是不可变数据类型,而value可以是任意数据类型. 字典通过一个花括号,里面存放key:value的数据结构来定义.理论上 ...

  8. IdentityServer4 中文文档 -3- (简介)已支持的规范

    IdentityServer4 中文文档 -3- (简介)已支持的规范 原文:http://docs.identityserver.io/en/release/intro/specs.html 目 录 ...

  9. DataGridView样式生成器使用说明

    啥都不说先看图 一.       功能介绍 1.      winform DataGridView样式代码可视化即时生成,所见即所得 2.      预置DataGridView样式代码方案 预置三 ...

  10. Oracle em 此网站的安全证书存在问题

    https://www.cnblogs.com/hyz5525/p/4390252.html C:\>emctl status dbconsole Oracle Enterprise Manag ...