购物登陆,列表生成式,生成器,斐波那契数列,yield伪并发,迭代器,时间模块,random模块

这次内容很多,比较重要

1、赋值方法--用元组、列表赋值

t = (123,8)            #元组赋值  (1,2,3)这种与元组数量不相同的赋值是不可以的
a,b = t
print(a)
print(b)

2、购物登陆(伪代码)

user = 'hanjie'
passwd = 123
w_user = 'hanjie123'
w_passwd = 123
login_states = False

def login():
if login_states== False:
if auth_type == "jingdong":
username = input ('please enter your username:')
password = input ('please enter your password :')
if user==username and passwd==password:
print('welcome...')
login_states = True
else:
pass
elif auth_type == "weixin":
username = input('please enter your username:')
password = input('please enter your password :')
if w_user==username and w_passwd==password:
print('welcome...')
login_states = True
else:
pass
else:
pass

@login
def home():
print("welcome to home page")


@login
def finance(auth_type="weixin"):
print("welcome to finance page")

@login
def book():
print("welcome to book page")


home()

pass------1、pass语句什么也不做,一般作为占位符或者创建占位程序,pass语句不会执行任何操作2、保证格式完整 3、保证语义完整

 

3、列表生成的另一种方法

这个地方的range如果很大,运行内存会直接暴毙,因为列表每有一个字符在里面就会占用一个内存空间

# a = [x*x for x in range(10)]          #先是在0到10里面去数字,然后依次x乘x放入到列表里面
# print(a)

4、生成器,只要是生成器就需要需要next,send去使用他

    1.在元组里面就不会形成内存 

    2.next的格式用法

    3.生成器的使用方法两个next(),还有右面的send

s = (x*2 for x in range(5))          #
print(s)
print(next(s)) #等价于s.__next__() in py2:s.next
print(next(s))
print(next(s))
print(next(s))
print(next(s))
print(next(s))

5、生成器

    1,。就是一个迭代对象

    2。生成器有两种创建方式

# 1.(x*2 for x in range(5))
# 2.yield

    3、函数function里面有yield,要在外面写上g=function,才能叫创建完生成器,要用next(g)才能一直迭代下去

 

 

 

6、yield的使用

  1.只要有yield那么就是生成器

  2.yield 的作用实现断层,把原先的程序向return一样出来,但是当他返回的时候还是在yield那一行,下一次利用nxet,或者send进来

  3.yield____  后面与return___都有一个返回值  

def foo():
print('ok')

yield 1 #返回值是1


print('ok2')
yield 2 #返回值是2

# g=foo()
# print(g) #<generator object foo at 0x0000000001DB9048> 只要有yield就是生成器

# a=next(g)
# b=next(g) #拿到1,2

# for i in foo():
# print(i) #这里的i只拿到1,2
      
                              这里的for循环相当于  ##     while True:
## i=next(foo())

 7、什么是可迭代对象------内部有lter方法的

# for i in [1,2,3]             #for 这个后面不一定是迭代器,是可迭代对象

8、斐波那契数列

#0 1 1 2 3 5 8 13 21
def fib(max):
n, before, after = 0, 0, 1
while n < max:
print(before)
yield before #变成了生成器对象
before, after = after, before + after #after, before + after先把这两个东西计算出来再赋值,所以看起来是同时进行
  #tmp=before
   #before=after
  # after=tmp+after
n = n + 1
return 'done'
g=fib(8)
print(g)

print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))

9、seed的引入

def bar():
print('ok1')
count=yield 1
print(count)

yield 2
b=bar()

# b.seed('None') #next(b) 第一次send前如果没有next,只能传一个send(None)
next(b)
b.send('eeee')

10、yield伪并发

import time
def consumer(name):
print("%s 准备吃包子啦!" %name)
while True:
baozi = yield #返回到调用者

print("包子[%s]来了,被[%s]吃了!" %(baozi,name))


def producer(name):
c = consumer('A')
c2 = consumer('B') #生成器对象c,c2
c.__next__()
c2.__next__()
print("老子开始准备做包子啦!")
for i in range(10):
time.sleep(1)
print("做了2个包子!")
c.send(i)
c2.send(i)

producer("alex")

 11、迭代器

生成器都是迭代器,迭代器不是生成器
# list,tuple,dict,string:Iterable(可迭代对象)
l = [1,2,3,5]
d = iter(l)

什么是迭代器?
满足两个条件:1,有iter方法 2,有next方法

 12、for循环的作用

for循环内部三件事:
1,调用可迭代对象的iter方法返回一个迭代器对象
2,不断调用迭代器对象的next方法
3,处理stopInteration
for i in [1,23,4]: #for循环相当于把[1,23,4]带入了iter()相当于形成了迭代器
iter([1,23,4])

 13、

from collections import Iterator,Iterable
l = [1,22,3,5]
d = iter(l)
# print(isinstance([1,3]),list) #判断类型是否正确
print(isinstance(l),Iterable)
print(isinstance(l)

 14、time模块

import time

# print(help(time))

# print(time.time()) #1581425541.1147807从linux诞生开始经过的时间:时间戳 !!!!
# time.sleep(3) !!!!
# print(time.clock()) #计算cpu执行时间
# print(time.gmtime()) #结构化时间-tm_year=2020, tm_mon=2, tm_mday=12, tm_hour=4, tm_min=48, tm_sec=7, tm_wday=2, tm_yday=43, tm_isdst=0 标准时区的时间
#print(time.localtime()) #当地时间-tm_year=2020, tm_mon=2, tm_mday=12, tm_hour=12, tm_min=50, tm_sec=46, tm_wday=2, tm_yday=43, tm_isdst=0
# struct_time=time.localtime()
# print(time.strftime('%Y--%m--%d %H:%M:%S',struct_time)) #时间的格式输入!!!!!!!
# print(help(time.strftime))

# print(time.strptime('2020--02--11 21:17:20','%Y--%m--%d %H:%M:%S')) #打印结构化时间,返回去
# a=time.strptime('2020--02--11 21:17:20','%Y--%m--%d %H:%M:%S')
# print(a.tm_year)
# print(a.tm_mday)
# print(a.tm_wday) !!!!

# 时间戳,结构化时间,格式化时间
#
# print(time.ctime()) #python自己的时间格式
#
# print(time.mktime(time.localtime())) #把标准的时间变成时间戳的模式

import datetime
print(datetime.datetime.now()) #标准的表达时间的方式

 15、random模块

import random
# print(random.random()) #0到1的随机数
print(random.randint(1,8)) #1-8的随机数
print(random.choice(['123',4,[1,2]])) #要什么随机可以出来

print(random.shuffle(['123',4,[1,2]]))
print(random.sample(['123',4,[1,2]],2)) #随机选两个数
print(random.randrange(1,10))

 16、验证码的生成

chr()把数字变成英文

def v_code():
code=''
for i in range(5):

add = random.choice([random.randrange(10),chr(random.randrange(65,91))])
# if i==random.randint(0,9)
# add_num=random.randrange(10)
# add_al=chr(random.randrange(65,91))
code+=str(add)
print(code)

 v_code()

 

 

 


posted @ 2020-02-12 00:00  凌风之沐  阅读(151)  评论(0)    收藏  举报