Python打卡第四周
這一周鴿了好久, 因為最近在準備比賽。課程一直沒跟上。。。
今天整理完本周的知識之后休息一周復習之前的
好了。
上總結
?
第一天
定義函數bar,在foo中調用
# def foo(): # # print('in the foo') # # bar() # # foo()def bar():print('in the bar')def foo():print('in the foo')bar()foo()?
在函數中嵌套一層,最內層的函數結束后必須在下面執行一次,要不然bar函數不會執行
def foo():print('in the foo')def bar():print('in the bar')bar() foo()?
使用timer裝飾器裝飾函數,并輸出運行時間
import timedef timmer(func):def warpper(*args,**kwargs):start_time = time.time()func()stop_time = time.time()print('the func run time is %s'%(stop_time-start_time))return warpper@timmer def test1():time.sleep(3)print('in the test1')test1()?
?
import time def timer(func):def deco(*args,**kwargs):star_time = time.time()func(*args,**kwargs)stop_time = time.time()print("the func run time is %s" %(stop_time-star_time))return deco@timer #test1 = timer(test1) def test1():time.sleep(3)print('in the test1')@timer def test2(name):print("test2 : ",name)test1() test2("Louis")?
?
第二天
?
生成器,可以直接把結果生成在一個函數中,也可以使用生成器的方式,將規則寫出來然后賦值給一個函數,這個函數只會保留地址,不會真的一次性賦值。等到輸出的時候使用__next__進行調用。
a=[0,1,2,3,4,5,6,7,8,9]c = (i*2 for i in range(1000))# for i in c: # print(i)print(c) c.__next__()?
將字符串類型轉換成迭代形式。使用iter函數
a = [1,2,3] b = iter(a) b.__next__()?
斐波那契數列
next:返回到迭代器
return:在程序函數中返回某個值,返回之后函數不在繼續執行,徹底結束。
yield:?帶有yield的函數是一個迭代器,函數返回某個值時,會停留在某個位置,返回函數值后,會在前面停留的位置繼續執行,直到程序結束
def fib(max):n,a,b=0,0,1while n<max:#print(b)yield ba,b =b,a+b # t = (b,a+b)n=n+1return '-----done-----'#f = fib(10) g = fib(10) while True:try:x = next(g)print('g:',x)except StopIteration as e:print('Generator return value:',e.value)break # print(f.__next__()) # print("------") # print(f.__next__()) # print(f.__next__()) # print(f.__next__()) # # print("=====start loop=====") # for i in f: # print(i)?
迭代器并行
import time def consumer(name):print("%s 準備吃包子"%name)while True:baozi = yieldprint("包子[%s]來了,被[%s]吃了" %(baozi,name))c = consumer("xiaoming") c.__next__() # b1="韭菜餡" # c.send(b1) #c.__next__()def producer(name):c = consumer('A')c2 = consumer('B')c.__next__()c2.__next__()print("老子開始準備做包子了!")for i in range(10):time.sleep(1)print("做了2個包子!")c.send(i)c2.send(i)producer("louis")?
第三天
json模塊:只適用于簡單的數據類型,是一種跨平臺的模塊。
pickle模塊:能夠轉換傳遞復雜的數據類型,是Python特有的一種數據類型。
#import json#不同語言的程序進行交互 import pickle def sayhi(name):print("hello",name)info ={'name':'louis','age':18'func':sayhi } f = open("test.txt","wb") #f.write(str(info))#f.write(json.dumps(info)) f.write(pickle.dumps(info)) f.close()?
反序列化
使用json的時候我們可以使用load方法來代替loads方法:只需要將f.write(json.dumps(info))替換成json.dump(info,f)就可以了
#import json import pickledef sayhi(name):print("hello",name) f = open("test.txt","rb")# data = eval(f.read()) # f.close() # print(data['age']) data = pickle.loads(f.read()) print(data["func"]("louis"))?
import pickledef sayhi(name):print("hello2",name)f = open("test.txt","rb") data = pickle.load(f) print(data["func"]("louis"))?
第四天
?
第五天
軟件目錄結構規范
bin/: 存放項目的一些可執行文件,當然你可以起名script/之類的也行。
"自定義文件名"/: 存放項目的所有源代碼。
docs/: 存放一些文檔。
setup.py: 安裝、部署、打包的腳本。
requirements.txt: 存放軟件依賴的外部Python包列表。
README: 項目說明文件。
?
總結:這一周不怎么在狀態,決定休息一周,重新復習一遍之前所學的內容。盲目的前進會事倍功半。
轉載于:https://www.cnblogs.com/yuanjun93/p/10758666.html
總結
以上是生活随笔為你收集整理的Python打卡第四周的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Saiku Table展示数据合并bug
- 下一篇: Python 常见的内置模块