傳遞列表
- 將列表傳遞給函數(shù)后,函數(shù)就可以直接訪問其內(nèi)容。
def greet_users(names):
for name in names:
mgs = "Hello " + name.title() + "."
print(mgs)
usernames = ['harry','bobo','sky']
greet_users(usernames)
Hello Harry.
Hello Bobo.
Hello Sky.
[Finished in 0.1s]
在函數(shù)中修改列表
- 將函數(shù)傳遞給列表,函數(shù)就可以對其進(jìn)行修改。
- 函數(shù)對列表所做的任何修改都是永久性的。
unprinted_desigins = ['iphonr case','rebot pendent','dodecaheron']
completed_models = []
while unprinted_desigins:
current_desigin = unprinted_desigins.pop()
print("Printing model: " + current_desigin)
completed_models.append(current_desigin)
print("\nThe following model have been printed: ")
for completed_model in completed_models:
print(completed_model)
Printing model: dodecaheron
Printing model: rebot pendent
Printing model: iphonr case
The following model have been printed:
dodecaheron
rebot pendent
iphonr case
[Finished in 0.1s]
- 編寫兩個函數(shù),
- 一個函數(shù)負(fù)責(zé):打印設(shè)計工作
- 另一個函數(shù)負(fù)責(zé)打印了那些設(shè)計
def print_models(unprinted_desigins,completed_models):
while unprinted_desigins:
current_desigin = unprinted_desigins.pop()
print("Printing model: " + current_desigin)
completed_models.append(current_desigin)
def show_completed_models(completed_models):
print("\nThe following model have been printed: ")
for completed_model in completed_models:
print(completed_model)
unprinted_desigins = ['iphonr case','rebot pendent','dodecaheron']
completed_models = []
print_models(unprinted_desigins,completed_models)
show_completed_models(completed_models)
Printing model: dodecaheron
Printing model: rebot pendent
Printing model: iphonr case
The following model have been printed:
dodecaheron
rebot pendent
iphonr case
[Finished in 0.1s]
禁止在函數(shù)中修改列表
- 使用切片發(fā)[:]創(chuàng)建列表的副本。
- 向函數(shù)傳遞列表的副本而不是原件,這樣函數(shù)的任何修改就只影響副本,而不影響原件。
傳遞任意數(shù)量的參數(shù)
- 使用*形參名,允許函數(shù)調(diào)用語句中收集任意實參。
def make_pizza(*toppings):
print(toppings)
make_pizza("aa")
make_pizza("mushrooms",'green peppers')
('aa',)
('mushrooms', 'green peppers')
[Finished in 0.1s]
- 對列表進(jìn)行遍歷
def make_pizza(*toppings):
print("\nMakeing a pizza with the follwing toppings: ")
for topping in toppings:
print("- " + topping)
make_pizza("aa")
make_pizza("mushrooms",'green peppers')
Makeing a pizza with the follwing toppings:
- aa
Makeing a pizza with the follwing toppings:
- mushrooms
- green peppers
[Finished in 0.1s]
結(jié)合使用位置實參和任意數(shù)量實參
- 讓函數(shù)接受不同類型的實參,必須在函數(shù)定義中將接納任意數(shù)量實參的形參放在最后面。
- python優(yōu)先匹配位置實參和關(guān)鍵字實參
def make_pizza(size,*toppings):
# print("\nMakeing a pizza with the follwing toppings: ")
print("\nMakeing a " + str(size) +
"-inch pizza with the follwing topings: ")
for topping in toppings:
print("- " + topping)
make_pizza(8,"mushrooms")
make_pizza(9,"mushrooms",'green peppers','extra cheese')
Makeing a 8-inch pizza with the follwing topings:
- mushrooms
Makeing a 9-inch pizza with the follwing topings:
- mushrooms
- green peppers
- extra cheese
[Finished in 0.1s]
使用任意數(shù)量的關(guān)鍵字實參
- 接受任意數(shù)量的實參時,預(yù)先不知道會傳遞給函數(shù)什么樣的信息。
- 這種情況可將函數(shù)編寫成能接受任意數(shù)量的鍵-值對,**形參名
def build_user(first,last,**user_info):
profile = {}
profile['first_name'] = first
profile['last_name'] =last
for key,value in user_info.items():
profile[key] = value
return profile
user_profile = build_user("kami",'lalun',location="pricetion",filed='physics')
print(user_profile)
{'first_name': 'kami', 'last_name': 'lalun', 'location': 'pricetion', 'filed': 'physics'}
[Finished in 0.1s]
- **user_info兩個星號讓python創(chuàng)建了一個名為user-info的字典
將函數(shù)存儲在模塊中
函數(shù)的優(yōu)點,可以代碼塊與主程序分離,
將函數(shù)存儲在稱之為模塊的獨立文件中,然后再impor導(dǎo)入模塊。
存儲模塊,1.可以隱藏代碼細(xì)節(jié),將重點放在程序的更高邏輯上。2.在不能程序中重用函數(shù)。3.與其他程序員共享文件而不是整個程序。
導(dǎo)入模塊
- 創(chuàng)建一個模塊pizza.py
def make_pizza(size,*toppings):
print("\nMakeing a " + str(size) +
"-inch pizza with the follwing topings: ")
for topping in toppings:
print("- " + topping)
- 在pizza.py的文件創(chuàng)建另一個名為makeing_pizza.py的文件、
import pizza
pizza.make_pizza(9,'moshrooms')
pizza.make_pizza(100,'aa','bb','cc')
Makeing a 9-inch pizza with the follwing topings:
- moshrooms
Makeing a 100-inch pizza with the follwing topings:
- aa
- bb
- cc
[Finished in 0.1s]
導(dǎo)入特定函數(shù)
- 導(dǎo)入模塊中的特定的函數(shù)
from module_name import function_name
- 用逗號隔開函數(shù)名,從模塊導(dǎo)入任意需要數(shù)量的函數(shù)。
from module_name import function_name1,function_name2,function_name3
from pizza import make_pizza
make_pizza(9,'aa')
Makeing a 9-inch pizza with the follwing topings:
- aa
[Finished in 0.1s]
使用as給函數(shù)指定別名
- 關(guān)鍵字as將函數(shù)重命名為你提供的別名。
from pizza import make_pizza as mp
mp(9,'aa')
Makeing a 9-inch pizza with the follwing topings:
- aa
[Finished in 0.1s]
用as給模塊指定別名
import module_name as mn
import pizza as mp
mp.make_pizza(9,'aa')
Makeing a 9-inch pizza with the follwing topings:
- aa
[Finished in 0.1s]
導(dǎo)入模塊中所有的函數(shù)。
- 使用星號(*)讓python導(dǎo)入模塊中所有的函數(shù)。
from pizza import *
make_pizza(100,'moshrooms')
Makeing a 100-inch pizza with the follwing topings:
- moshrooms
[Finished in 0.1s]
函數(shù)編寫指南
- 給函數(shù)指定描述性名稱。
- 每個函數(shù)都要闡述七功能的注釋。
- 給形參指定默認(rèn)值時,等號兩邊不要有空格。
def function_name(parameter_0,parameter='default value')
- 函數(shù)調(diào)用的關(guān)鍵字實參,同樣等號兩邊不要有空格。
function_name(value_0,patameter='value_1')
總結(jié)
- 如何編寫函數(shù)
- 如何傳遞實參。
- 如何使用位置實參和關(guān)鍵字實參,以及接受任意數(shù)量的實參
- 顯示輸出的函數(shù)和返回值函數(shù)。
- 如何將函數(shù)通列表,字典、if語句和while語句結(jié)合使用