python_day17_迭代器_时间模块_随机数模块

        2018年的十一假期已经过去四天了,本来想好的专利的初稿写好了,就出去玩一下的,至少去一趟西湖,不知道是因为懒的出去,还是因为今天上午到实验室的时候,突然停电了,然后手机没电,就没去;傍晚的时候好像有点想明天去一下西湖,哎呀,还是看明天的心情吧,随机而动。九点的时候,还是没忍住把专利的初稿发给导师了,现在还没回我,其实我已经做好各种准备了,知道自己写的烂,心里还是有点number滴。今天早上在宿舍门旁边摘了一小朵桂花,太香了,突然想养一个桂花的盆栽,期待~~

#Author:"haijing"
#date:2018/10/4

#生成器就是迭代器,但迭代器不一定都是生成器
#迭代器:(1)有iter()方法,(2)有next()方法
#可迭代对象:list、dic、tuple、string
k=[1,2,3,4.5]
d=iter(k) #d就是一个迭代器
print(d) #<list_iterator object at 0x00C06370>

d1=next(d) #d1是一个迭代器
d2=next(d)
print(d1,d2)

print(isinstance([1,2.3],list)) #判断[1,2.3]是否是一个列表list,如果是返回true
print(isinstance([12,3,],Iterable)) #判断[12,3,]是否为可迭代对象
print(isinstance(d,Iterator)) #判断d是否为迭代器


#Author:"haijing"
#date:2018/10/4

#时间戳:用秒来表示,从1970年unix诞生,到现在有多少秒
import time #import用来调用某一模块
# print(time.time()) #时间戳 打印1538644663.0587497
# print(time.clock()) #计算cpu工作的时间,但是在执行time.sleep时cpu并不工作
# print(time.gmtime()) #打印UTC伦敦时间 tm_year=2018, tm_mon=10, tm_mday=4, tm_hour=9, tm_min=23, tm_sec=5, tm_wday=3, tm_yday=277, tm_isdst=0)
# print(time.localtime()) #打印本地时间
# print(time.strftime('%Y--%m--%d %H:%M:%S')) #2018--10--04 17:33:57

# a=time.localtime()
# print(time.mktime(a))


#最常用的表示时间的语句
import datetime
print(datetime.datetime.now()) #2018-10-04 20:36:44.426046

#Author:"haijing"
#date:2018/10/4

import random

# print(random.random()) #打印0-1的随机数
# print(random.randint(1,8)) #打印1-8的随机数,包括8
# print(random.choice('hello world')) #随机打印hello world,包括中间的那个空格
# print(random.choice(['min',4,[1,2]])) #随机打印'min',4,[1,2]]中的一个
# print(help(random.shuffle)) #查看random.shuffle的用法
# print('min',123,[1,2],a) #在'min',123,[1,2]中随机的选a个数
# print(random.randrange(1,3)) #随机打印1-3中的一个数


#产生验证码
# def v_code():
# code = '' #产生一个空字符串
# for i in range(5):
# if i==random.randint(0,3): #random.randint(0,3)随机产生0-3
# add = random.randrange(10)
# else:
# add = chr(random.randrange(65, 90))
# code += str(add) #将随机产生的数字转换成字符串,并加紧add_num中
# print(code) #随机打印0-9数字
#
# v_code()
# print(chr(90)) #65-90表示小写字母的asc码 90代表z,所以这一句打印z

#但是以上产生的验证码几乎全是字母,改进如下
def v_code():
code = '' #产生一个空字符串
for i in range(5):
# add = random.choice(random.randrange(10),chr(random.randrange(65, 90))) #这样写不对,要用一个列表要盛随机选的数字和字母
add = random.choice([random.randrange(10), chr(random.randrange(65, 90))])
code += str(add) #将随机产生的数字转换成字符串,并加紧add_num中
print(code) #随机打印0-9数字和字母
v_code()
 
 
原文地址:https://www.cnblogs.com/YiYA-blog/p/9743615.html