例えば:
def greet_user():
"""显示简单的问候语"""
print("Hello!")
greet_user()
まず、関数定義:
1、キーワードを使用します。defのpythonの関数を定義します。
2、関数名:関数名のgreet_user。Pythonでの代わりに、一般的にこぶの表現に使用されるJavaを使用する、異なる単語を分離するために、「_」アンダースコアを使用。
図3に示すように、関数パラメータ:関数パラメータは、関数名の後の括弧内に添加されます。
図4に示すように、関数の終了が定義され:コロン「:」最後まで。
5、身体機能:コロンの後の身体の関数として示します。関数の本体ではなく、道を定義するために括弧を使用するかどうかを関数の本体は、区別するためにインデントを使用する必要があります。
6、文書の文字列:二重引用符の3組のドキュメントの文字列(のドキュメンテーション文字列)、機能をマーキングすることを目的。
第二に、伝送パラメータ:
図1に示すように、配信場所引数:引数及びパラメータ必要な位置一貫。
2、キーワード引数を渡す:各パラメータには、変数名と値で構成されました。
3、あなたはまた、リストや辞書を使用することができます。
図4に示すように、デフォルト値:関数呼び出しは、パラメータは、デフォルト値に割り当てられていない場合、各パラメータについては、関数定義のデフォルト値を割り当てることができます。
デフォルト値は、後ろに(複数の場合もある)しなければならないことに留意されたいです。
場所引数:
def describe_pet(animal_type, pet_name):
"""显示宠物的信息"""
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet('hamster', 'harry')
キーワード引数:
def describe_pet(animal_type, pet_name):
"""显示宠物的信息"""
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet(animal_type='hamster', pet_name='harry')
第三に、戻り値:
あなたは、関数を呼び出すコードの行に値を返すためにreturnステートメントを使用することができます。
def get_formatted_name(first_name, last_name, middle_name = ''):
"""返回整洁的姓名"""
if middle_name:
full_name = first_name + ' ' + middle_name +' ' + last_name
else:
full_name = first_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('jimi', 'hendrix', 'lee')
print(musician)
第四に、転送リスト:
def greet_users(names):
"""向列表总的每位用户都发出简单的问候"""
print()
for name in names:
msg = "Hello, " + name.title() + "!"
print(msg)
usernames = ['hannah', 'ty', 'margot']
greet_users(usernames)
第五に、変更の機能一覧:
def print_models(unprinted_designs, completed_models):
"""
模拟打印每个设计, 直到没有未打印的设计为止
打印每个设计后, 都将其移到列表completed_models中
"""
while unprinted_designs:
current_design = unprinted_designs.pop()
#模拟根据设计制作3D打印模型的过程
print("Printing model:" + current_design)
completed_models.append(current_design)
def show_completed_models(completed_models):
"""显示打印好的所有模型"""
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs,completed_models)
show_completed_models(completed_models)
第六に、リストを修正するための禁止機能:
Aはリストを変更するために禁止機能を実現するための関数に渡すことができ、元のリストではなくコピーします。
使用Pythonのスライスリスト名[:]コピーを作成します。
print_models(unprinted_designs[:], completed_models)
セブン、任意の数の引数を渡します。
def make_pizza(*toppings):
"""概述要制作的比萨"""
print("\nMaking a pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
どこのPythonを作るために、アスタリスク*トッピングトッピングという名前の空のタプルを作成し、すべての値がこのタプルを受け取るためにカプセル化されています。
Pythonの引数は、受信関数値も真であっても、タプルにパッケージ。
八、引数の引数の任意の数と位置と連動して:
あなたは、関数の引数の種類を受け入れるようにしたい場合、あなたは最後の形状パラメータの関数定義の任意の数の引数を受け入れる必要があります。Pythonの最初の引数とキーワードマッチング位置引数は、残りの引数は、最後のパラメータに集められます。
def make_pizza(size, *toppings):
"""概述要制作的比萨"""
print("\nMaking a " + str(size) +
"-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
九、キーワード引数の任意の数:
def build_profile(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_profile('albert', 'einstein',
location = 'princeton',
field = 'physics')
print(user_profile)
: - この辞書の中にパッケージのすべての値をパラメータ** USER_INFO 2つのアスタリスクは、辞書Pythonはすべての名前USER_INFOと命名し、受信した作成空にしてみましょう。