1 ##---------------------- 函式 ---------------------- 2 3 ##1 定義函式 4 def hello_py(): 5 print('hello,python!') 6 7 hello_py() 8 ##1.1向函式傳遞訊息 9 def get_message(username): 10 print('Hello,'+username.title()+'!') 11 name=input('請輸入你的姓名:') 12 get_message(name) 13 ##1.2實參和形參 14 ''' 15 前面定義函式 get_message() 時,要求給變數 username 指定一個值,呼叫這個函式并提供這種 16 資訊(人名)時,它將列印相應的問候語, 17 在函式 get_message() 的定義中,變數 username 是一個形參——函式完成其作業所需的一項信 18 息,在代碼 get_message(name) 中,值 name 是一個實參,實參是呼叫函式時傳遞給函式的信 19 息,我們呼叫函式時,將要讓函式使用的資訊放在括號內,在 get_message(name) 中,將實參 20 name的值 傳遞給了函式 get_message() ,這個值被存盤在形參 username 中, 21 22 形參和實參位置順序要一一對應 23 函式也可以多次呼叫 24 基本上與C語言一樣 25 ''' 26 ##1.3默認值 27 ''' 28 撰寫函式時,可給每個形參指定默認值,在呼叫函式中給形參提供了實參時,Python將使用 29 指定的實參值;否則,將使用形參的默認值,因此,給形參指定默認值后,可在函式呼叫中省略 30 相應的實參,使用默認值可簡化函式呼叫,還可清楚地指出函式的典型用法, 31 ''' 32 def describe_pet(pet_name,animal_type='哈士奇'): 33 #顯示寵物的資訊 34 print('我在家養過寵物,我給它起名叫做:'+pet_name) 35 print(pet_name+'是一只非常好的'+animal_type) 36 describe_pet('神奇')##第二個引數使用默認值 37 38 ###1.4各種呼叫方法 39 describe_pet('傳奇') 40 describe_pet(pet_name='傳奇') 41 describe_pet('可可','貓') 42 name='巧巧' 43 an_type='貓' 44 describe_pet(name,an_type)##利用別的變數傳遞 45 ##1.5回傳簡單值 46 def get_formatted_name(first_name,last_name): 47 full_name=last_name+' '+first_name 48 return full_name.title() 49 musician=get_formatted_name('復','慕容') 50 print(musician) 51 ##1.5讓實參變成可選的 52 ''' 53 有時候,需要讓實參變成可選的,這樣使用函式的人就只需在必要時才提供額外的資訊,可 54 使用默認值為空來讓實參變成可選的, 55 ''' 56 ##回傳字典 57 def build_person(first_name,last_name,age=''):##此處利用age默認值為空實作了引數的可選擇性 58 ##回傳一個在字典,其中包括一個人資訊 59 person={'first':first_name,'last':last_name} 60 if age: 61 person['age']=age 62 return person 63 musician=build_person('鋒','歐陽') 64 print(musician) 65 print('---------------------------------') 66 ##函式和while回圈的結合 67 circulation=True##回圈變數 68 while circulation: 69 print('輸入你的名字和姓氏:') 70 f_name=input('first_name:') 71 l_name=input('last_name:') 72 full_name=build_person(f_name,l_name) 73 print('你好'+str(full_name['first']+str(full_name['last']))) 74 print('后面,還有小朋友嗎?(yes/no)') 75 answer=input() 76 if answer=='no' or answer=='NO' or answer=='n': 77 circulation=False 78 79 ##傳遞串列 80 print('-----------------------------------------') 81 def greet_user(names): 82 for name in names: 83 msg='hello'+name.title()+'!' 84 print(msg) 85 86 user_list=['小明','小華','杰克'] 87 greet_user(user_list) 88 ##在函式中修改串列 89 print('---------------------------------------------') 90 91 92 ##函式后序內容 93 ''' 94 將串列傳遞給函式后,函式就可對其進行修改,在函式中對這個串列所做的任何修改都是永 95 久性的,這讓你能夠高效地處理大量的資料, 96 來看一家為用戶提交的設計制作3D列印模型的公司,需要列印的設計存盤在一個串列中, 97 列印后移到另一個串列中, 98 ''' 99 def print_models(unprinted_designs,completed_models): 100 ##列印每個設計后,都將其移到串列completed_models中 101 while unprinted_designs: 102 current_design=unprinted_designs.pop() 103 104 ##模擬根據要求的列印程序 105 print('正在列印中的模型是:'+current_design) 106 completed_models.append(current_design) 107 def show_completed_models(completed_models): 108 ##顯示所有列印好的模型 109 print('列印完成的模型串列:\n') 110 for print_over_models in completed_models: 111 print('\t'+print_over_models) 112 113 unprinted_designs=['160_轟炸機','米格——3k攔截機','蘇——57戰斗機','F22戰斗機'] 114 completed_models=[] 115 print_models(unprinted_designs,completed_models) 116 print('\n\n') 117 show_completed_models(completed_models) 118 119 ##禁止函式修改串列(利用串列切片實作) 120 ##例:print_models(unprinted_designs[:],completed_models[:]) 121 122 ##傳遞任意數量的引數 123 def make_pizza(*topping): 124 ##其中實參*topping是一個元組,所以可以傳遞任意數量的引數 125 '''列印顧客點的所有配料''' 126 print(topping) 127 128 make_pizza('洋蔥') 129 make_pizza('洋蔥','雞蛋','面粉','培根') 130 131 ##使用任意數量的關鍵字實參 132 ##其中,下面的**user_info可以理解為一個字典 133 def build_profile(first,last,**user_info): 134 '''創建一個字典,其中包含我們知道的有關用戶的一切''' 135 profile={} 136 profile['first_name']=first 137 profile['last_name']=last 138 for key,value in user_info.items(): 139 profile[key]=value 140 return profile 141 142 user_profile=build_profile('趙','飛燕',性別='女',職業='舞蹈藝術家') 143 print(user_profile)
將函式存盤在模塊中:
下面有兩個.py檔案(pizza.py和making_pizza.py)用來演示函式模塊的匯入方式
1 #將函式存盤在模塊中 2 def make_pizza(size,*toppings): 3 ##概述披薩的制作要求: 4 print('披薩的尺寸是:'+str(size)+'以及需要的配料有:') 5 for topping in toppings: 6 print('\t'+str(topping)) 7
1 pizza.make_pizza(10,'小麥','牛奶','洋蔥','雞蛋') 2 3 ##匯入特定的函式 4 ##你還可以匯入模塊中的特定函式,這種匯入方法的語法如下: 5 #form module _name import function_name 6 ##通過用逗號分隔函式名,可根據需要從模塊中匯入任意數量的函式: 7 #form module_name import function_name1,function_name2,function_name3 8 9 ##使用as可以給函式起別名 10 '''如果要匯入的函式的名稱可能與程式中現有的名稱沖突,或者函式的名稱太長,可指定簡短 11 而獨一無二的別名——函式的另一個名稱,類似于外號,要給函式指定這種特殊外號,需要在導 12 入它時這樣做, 13 ''' 14 from pizza import make_pizza as mp 15 mp(10,'面粉') 16 17 18 19 ##也可以使用as給模塊起別名 20 import pizza as pa 21 p.make_pizza(10,'水') 22 23 24 ##匯入模塊中的所有函式 25 from pizza import * 26 '''import 陳述句中的星號讓Python將模塊 pizza 中的每個函式都復制到這個程式檔案中,由于匯入 27 了每個函式,可通過名稱來呼叫每個函式,而無需使用句點表示法,然而,使用并非自己撰寫的 28 大型模塊時,最好不要采用這種匯入方法:如果模塊中有函式的名稱與你的專案中使用的名稱相 29 同,可能導致意想不到的結果:Python可能遇到多個名稱相同的函式或變數,進而覆寫函式,而 30 不是分別匯入所有的函式,最佳的做法是,要么只匯入你需要使用的函式,要么匯入整個模塊并 31 使用句點表示法,這能讓代碼更清晰,更容易閱讀和理解, 32 '''
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/147654.html
標籤:Python
上一篇:2020年Java基礎高頻面試題匯總(1.4W字詳細決議,你能遇到的都在這了)
下一篇:python基礎一
