8 函數(shù)

# ~ def greet_user():

? ? # ~ """打印Hello"""

? ? # ~ #文檔字符串(docstring)的注釋,描述函數(shù)作用,三個引號括起,用來生成程序中函數(shù)的文檔

? ? # ~ print('Hello')

# ~ greet_user()

# ~ def greet_user(user_name):

? ? # ~ print("Hello, "+user_name.title()+"!")

# ~ greet_user('jerry')

# ~ def describe_pet(type,name):

? ? # ~ """顯示寵物信息"""

? ? # ~ print("\nI have a "+type+".")

? ? # ~ print("My "+type+"'s name is "+name.title()+'.')


# ~ describe_pet('hamster','harry')

#位置實參

#形參和實參關聯(lián)方式基于實數(shù)的順序

# ~ describe_pet(type='hamster',name='harry')

# ~ describe_pet(type='harry',name='hamster')

#關鍵字實參,順序可忽略

# ~ def describe_pet(name,type='dog'): #形參指定默認值

? ? # ~ print("\nI have a "+type+".")

? ? # ~ print("My "+type+"'s name is "+name.title()+'.')? ?

# ~ describe_pet(name='harry')

# ~ describe_pet('mili')

# ~ def format_name(first_name,last_name):

? ? # ~ full_name = first_name+' '+last_name

? ? # ~ return full_name.title()

# ~ music = format_name('liu','longfei')

# ~ print(music)

# ~ def format_name(first_name,last_name,middle_name=''):

? ? # ~ if middle_name: #Python將非空字符串解讀為True

? ? ? ? # ~ full_name = first_name+' '+middle_name+' '+last_name

? ? # ~ else:

? ? ? ? # ~ full_name = first_name+' '+last_name

? ? # ~ return full_name.title()

# ~ music = format_name('liu','long','fei')

# ~ print(music)

# ~ music = format_name('liu','longfei')

# ~ print(music)

#-----------------------------------------------------

#返回字典

# ~ def build_person(first_name,last_name):

? ? # ~ person = {'first':'first_name','last':'last_name',}

? ? # ~ return person

# ~ music = build_person('liu','shuai')

# ~ print(music)

#-----------------------------------------------------

#結合使用函數(shù)和 while 循環(huán)

# ~ def format_name(first_name,last_name):

? ? # ~ full_name = first_name+' '+last_name

? ? # ~ return full_name.title()

# ~ while True:

? ? # ~ print("\nPlease tell me your name:")

? ? # ~ print("(enter 'q' at any time to quit)")


? ? # ~ f_name = input("First name:")

? ? # ~ if f_name =='q':

? ? ? ? # ~ break

? ? # ~ l_name = input("Last name:")

? ? # ~ if l_name == 'q':

? ? ? ? # ~ break

# ~ format_name0 = format_name(f_name,l_name)

# ~ print("\nHello,"+format_name0+'!')

#-----------------------------------------------------

#傳遞列表

# ~ def users(names):

? ? # ~ for name in names:

? ? ? ? # ~ msg = 'Hello, '+name.title()

? ? ? ? # ~ print(msg)

# ~ username = ['A','B','C']

# ~ users(username)

#-----------------------------------------------------

#未使用函數(shù)修改列表

# ~ unprinted_designs = ['iphone','samsang','huawei']

# ~ completed_models = []

# ~ while unprinted_designs:

? ? # ~ completed_model = unprinted_designs.pop()


? ? # ~ print("Current:"+completed_model)

? ? # ~ completed_models.append(completed_model)

# ~ print('completed_models has printed')

# ~ for xx in completed_models:

? ? # ~ print(xx)

#在函數(shù)中修改列表

# ~ def remove(unprinted_designs,completed_models):

? ? # ~ while unprinted_designs:

? ? ? ? # ~ completed_model = unprinted_designs.pop()

? ? ? ? # ~ print("Current:"+completed_model)

? ? ? ? # ~ completed_models.append(completed_model)

# ~ def show_model(completed_models):

? ? # ~ print('completed_models has printed')

? ? # ~ for xx in completed_models:

? ? ? ? # ~ print(xx)

# ~ unprinted_designs = ['iphone','samsang','huawei']

# ~ completed_models = []

# ~ remove(unprinted_designs,completed_models)

# ~ show_model(completed_models)

#禁止函數(shù)修改列表

#function_name(list_name[:]) 將列表的副本傳遞給函數(shù)

# ~ unprinted_designs = ['iphone','samsang','huawei']

# ~ completed_models = []

# ~ remove(unprinted_designs[:],completed_models)

# ~ print(unprinted_designs)

# ~ print(completed_models)

#-----------------------------------------------------

# *形參,傳遞任意數(shù)量(包括0)的實參

#形參名*toppings中的*讓Python創(chuàng)建名為toppings的空元組,并將收到的所有值都封裝到這個元組中

# ~ def make_pizza(*toppings):

? ? # ~ print('\nMaking a pizza with following toppings')

? ? # ~ for top in toppings:

? ? ? ? # ~ print('- '+top)

# ~ make_pizza('mushroom')

# ~ make_pizza('apple','orange','banana')

# ~ def make_pizza(size,*toppings):

? ? # ~ print('\nMaking a '+str(size)+'-inch pizza')

? ? # ~ for top in toppings:

? ? ? ? # ~ print('- '+top)

# ~ make_pizza(12)

# ~ make_pizza(12,'mushroom')

# ~ make_pizza(12,'apple','orange','banana')

# **形參,傳遞任意數(shù)量(包括0)的鍵值對

#形參**讓Python創(chuàng)建名為user_info的空字典,將收到的所有名稱—值對都封裝到這個字典中

# ~ def build_profile(first,last,**info):

? ? # ~ profile = {}

? ? # ~ profile['first_name'] = first

? ? # ~ profile['last_name'] = last

? ? # ~ for key,value in info.items():

? ? ? ? # ~ profile[key] = value

? ? # ~ return profile

# ~ user_profile = build_profile('li','ming',age=24,sex='male')

# ~ print(user_profile)

#-----------------------------------------------------

# 將函數(shù)存儲在模塊(獨立文件)中

# 導入整個模塊

# ~ import function?

# ~ function.make_pizza(16)

# ~ function.make_pizza(16,'mushroom','green peppers')

# 給模塊制定別名

# ~ import function as f

# ~ f.make_pizza(16)

# ~ f.make_pizza(16,'mushroom','green peppers')

# 導入特定的函數(shù),調用時無需指定模塊名

# from module_name import function_name

# 導入一個函數(shù)

# from module_name import function_0, function_1, function_2

# 導入任意個函數(shù)

# ~ from function import make_pizza

# ~ make_pizza(16)

# ~ make_pizza(16,'mushroom','green peppers')

# as給函數(shù)指定別名

# ~ from function import make_pizza as mp

# ~ mp(16)

# ~ mp(16,'mushroom','green peppers')

# *導入模塊所有函數(shù)

from function import *

make_pizza(16)

make_pizza(16,'mushroom','green peppers')

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容