国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

python學(xué)習(xí)之函數(shù)和函數(shù)參數(shù)

2019-11-14 16:55:10
字體:
供稿:網(wǎng)友
 1 #方法的參數(shù)定義和默認(rèn)參數(shù)的定義 2 def ask_ok(PRompt, retries=4, complaint='Yes or no, please!'): 3        while True: 4               ok = input(prompt) 5               if ok in ('y', 'ye', 'yes'): 6                    print("true"); 7               if ok in ('n', 'no', 'nop', 'nope'): 8                      return False 9                      retries = retries - 110               if retries < 0:11                    raise OSError('uncoOperative user');#相當(dāng)于c#中的throw;12                    print(complaint)13                    14 #方法的調(diào)用15 def show():16        ask_ok("please input docstring");17 18 19 #默認(rèn)值的共享20 def  fun(n,args=[]):21         args.append(n);22         return args;23 24 print(fun(2));    25 print(fun(3));#長(zhǎng)度加126 print(fun(4));#長(zhǎng)度加127 28 29 30 #除去默認(rèn)值的累計(jì),->為注釋,表示方法的返回值類型31 def  func(n,args=None)->[]:32         if args is None:33              args=[];34         args.append(n);35         return args;36     37 print(func(2));    38 print(func(3));#長(zhǎng)度139 print(func(4));#長(zhǎng)度140 41 42 43 44 #*當(dāng)最后一個(gè)形參以 **name 形式出現(xiàn),它接收一個(gè)字典 (見映射類型 —— 字典) ,該字典包含了所有未出現(xiàn)在形式參數(shù)列表中的關(guān)鍵字參數(shù)。45 #它還可能與 *name 形式的參數(shù)(在下一小節(jié)中所述)組合使用,*name 接收一個(gè)包含所有沒有出現(xiàn)在形式參數(shù)列表中的位置參數(shù)元組。46 #(*name 必須出現(xiàn)在 ** name 之前。)例如,如果我們定義這樣的函數(shù):47 48 def getInfo(info,*argument,**keyWords):49        print("this is ",info);50        for msg in argument:51              print(msg);52        for kw in keywords:53              print(kw,":",keywords[kw]);54                55 getInfo("Limburger", "It's very runny, sir.",56            "It's really very, VERY runny, sir.",57            shopkeeper="Michael Palin",58            client="John Cleese",59            sketch="Cheese Shop Sketch");60 61 #參數(shù)的分拆62 def parrot(name="xiaoxiao",age=24):63        print("my name is",name,end='');64        print("and my age is ",age,end='');65 66 student={"name":"jack","age":25};67 parrot(**student);68 69 stu=["jack",25];70 parrot(*stu);71 72 #pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]73 #pairs.sort(key=lambda pair: pair[1])74 75 #lamda表達(dá)式的使用,就是逆名函數(shù)76 def sorter(L=None):77        if L is None:78             L=[];           79        L.sort(key=lambda pair:pair[1]);80        return L;81     82 L = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')];83 print(sorter(L));84 85 #文檔字符串86 def myfunction():87        """Do nothing,,,but88        No,really,,,it doesn't89        """;90        pass;91 92 print(myfunction.__doc__);    

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 独山县| 房山区| 水富县| 凤庆县| 耒阳市| 余干县| 澄江县| 通榆县| 辽中县| 枣阳市| 陵川县| 桦川县| 新密市| 固镇县| 密云县| 镇安县| 吴旗县| 临武县| 寿阳县| 托克逊县| 锦屏县| 蕲春县| 司法| 石棉县| 顺义区| 新民市| 望江县| 中江县| 化隆| 宁津县| 安化县| 临夏县| 峨山| 鹿泉市| 澎湖县| 凌云县| 台州市| 华宁县| 黄大仙区| 会同县| 宜川县|