本帖最后由 つ兜兜里沒糖 于 2021-1-19 21:29 編輯
一、整理筆記
17:30--21:30
程序結(jié)構(gòu) 分為:順序結(jié)構(gòu),分支結(jié)構(gòu),循環(huán)結(jié)構(gòu) 分支結(jié)構(gòu) age = int(input ('請輸入您的年齡:')) if 語句可以單獨使用,else語句不能單獨使用 分支結(jié)構(gòu)中必須有if語句,并且只能有一個 ; Elif語句可有可無;else有的話只能有一個 循環(huán)結(jié)構(gòu) For循環(huán) for name in names : print (name) data = [1,2,3,4,5,6,7,8,9,10] data = list(range(11)) print (data) s=0 for n in data: for n in range(11): s = s + n print ('s=%d'%s) While循環(huán) #1+2+3+4+5+6+7+8+9+10 s = 0 n = 10 while n>0: s = s + n n = n - 1 print('s=%d'%s) break終止循環(huán) Continue 結(jié)束本次循環(huán) 字典 dict 是一組鍵key-值value對的集合 字典的查找速度特別快 字典的key是唯一的,但value可以重復(fù) 字典是無序的 #根據(jù)key獲取value score = name_scores['xiaobai'] print ('score=%d'%score) #get()獲取不到對應(yīng)的value時 ,則返回None score = name_scores.get('whj') print ('score=%s'%score) #添加鍵值對 name_scores ['bailina'] = 100 print (name_scores) #修改 name_scores['bailina'] = 90 print (name_scores) #刪除 name_scores.pop('bailina') print(name_scores) return 語句,返回函數(shù)執(zhí)行的結(jié)果 函數(shù)內(nèi)部執(zhí)行到return語句時,會立即結(jié)束,不要把任何語句寫在return語句之后 不寫return的時候,程序會默認的添加一個return語句,return后如果沒有任何返回值,相當(dāng)于return None 位置參數(shù)positional argument 位置參數(shù)必須傳參 位置參數(shù)是按照順序傳參的 默認參數(shù) -參數(shù)有一個默認值 -默認參數(shù)可以簡化函數(shù)的調(diào)用 -默認參數(shù)必須寫在位置參數(shù)的后面 -變化大的參數(shù)一般用位置參數(shù),變化小的參數(shù)可以作為默認參數(shù) import math def coordinate (x,y,lenth,angle): x1 = x + lenth*math.cos(angle) y1 = y - lenth*math.sin(angle) return x1,y1 #如果試圖在return語句中返回多個值 #程序自動將這多個值封裝為一個元組 PI = 3.1415926 result = coordinate(5,10,10,PI/6) print (result) x,y=(1,2) print (x,y)
|