日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python打卡第四周

發布時間:2023/12/13 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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序列化
  • 反序列化
  • 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"))

    ?

    第四天

  • 內置方法
  • print(all([1,-5,3]))#全真為真 print(any([1,-5,3]))#有一個真就是真 a = ascii([1,2,"測試"]) print(type(a),[a]) print(bin(255))#轉成二進制 print(bool(0))#布爾運算0假1真 b = bytes("abcde",encoding="utf-8") b1 = bytearray("abcde",encoding="utf-8")#可修改 print(b.capitalize(),b)#字符串不可以修改,字節形式也不行 print(b1[1])#打印ASCII碼 b1[1]=50 print(b1)def sayhi():pass print(callable(sayhi))print(chr(97))#返回ASCII碼字符 print(ord('a'))#返回ASCII碼值 code = "for i in range(10):print(i)" c = compile(code,'','exec') exec(c)# code1 = "1+5+8/2" # c1 = compile(code,'','eval') #eval(code) code1 = ''' 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 '''# py_obj = compile(code1,"err.log","exec") # exec(py_obj) exec(code1)a={}#dict{} print(dir(a))#查看字典的操作print(divmod(5,2))#取模 x =1 print(eval('x+1'))def sayhi1(n):print(n)for i in range(n):print(i) sayhi1(3) #(lambda n:print(n))(5) calc = lambda n:3 if n<4 else n print(calc(2))#res = filter(lambda n:n>5,range(10))#過濾器 #res = map(lambda n:n*n,range(10)) #res = [ lambda i: i*2 for i in range(10)] import functools res = functools.reduce(lambda x,y:x*y,range(1,10))#階乘 # for i in res: # print(i) print(res)a = frozenset([1,4,333,123,33,33,12,4])#不可變集合print(globals())#返回整個文件的keyvalueprint(hash('louis'))print(hex(255))def test():local_var = 333print(locals()) test() print(globals().get('local_var'))print(oct(10))#八進制 print(pow(2,8)) print(repr(c))#轉換成字符串對象print(round(1.33345,2))#保留x位小數位 d = range(20) print(d[slice(2,5)])k = {6:2,8:0,1:4,77:25,-5:6,99:11,4:22}#字典無序 print(sorted(k.items(),key = lambda x:x[1]))#之前按k排序,按value排序 print(k)a = [1,2,3,4,5,6] b = ['a','b','c','d'] for i in zip(a,b):print(i)#import decorator1 __import__('decorator1')

    ?

    第五天

    軟件目錄結構規范

    bin/: 存放項目的一些可執行文件,當然你可以起名script/之類的也行。

    "自定義文件名"/: 存放項目的所有源代碼。

  • 源代碼中的所有模塊、包都應該放在此目錄。不要置于頂層目錄。
  • ?其子目錄tests/存放單元測試代碼;
  • 程序的入口最好命名為main.py。
  • docs/: 存放一些文檔。

    setup.py: 安裝、部署、打包的腳本。

    requirements.txt: 存放軟件依賴的外部Python包列表。

    README: 項目說明文件。

  • 軟件定位,軟件的基本功能。
  • 運行代碼的方法: 安裝環境、啟動命令等。
  • 簡要的使用說明。
  • 代碼目錄結構說明,更詳細點可以說明軟件的基本原理。
  • 常見問題說明。
  • ?

    總結:這一周不怎么在狀態,決定休息一周,重新復習一遍之前所學的內容。盲目的前進會事倍功半。

    轉載于:https://www.cnblogs.com/yuanjun93/p/10758666.html

    總結

    以上是生活随笔為你收集整理的Python打卡第四周的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。