Python编程从入门到实践~函数
生活随笔
收集整理的這篇文章主要介紹了
Python编程从入门到实践~函数
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
定義函數(shù)
#定義函數(shù) def greet_user():print("Hello~")#調(diào)用函數(shù) greet_user()#向函數(shù)傳遞信息 def print_user(username):print(f"Hello~{username}")print_user('Jesse')傳遞實(shí)參數(shù)
#位置實(shí)參 def describe_pet(animal_type, pet_name):print(f"I have a {animal_type}")print(f"My {animal_type}'s name is {pet_name.title()}")describe_pet('hamster', 'harry')#關(guān)鍵字實(shí)參數(shù)&默認(rèn)值 def describe_peet(animal_type='haster', pet_name='harry'):print(f"I have a {animal_type}")print(f"My {animal_type}'s name is {pet_name.title()}")describe_peet() describe_peet(pet_name='Zhangsan', animal_type='human')返回值
#返回簡(jiǎn)單值 def get_formatted_name(first_name, last_name):full_name = f"{first_name} {last_name}"return full_name.title()musician = get_formatted_name('jimi', 'hendrix') print(musician)#讓實(shí)參數(shù)可選 def formatted_name(first_name, last_name, middle_name=''):full_name = f"{first_name} {middle_name} {last_name}"return full_name.title()musician = formatted_name('jimi', 'hendrix') print(musician) musician = formatted_name('jimi', 'hendrix', 'lee') print(musician)傳遞列表
#在函數(shù)中修改列表 def print_models(unprinted_designs, completed_models):while unprinted_designs:current_design = unprinted_designs.pop()print(f"Printing model:{current_design}")completed_models.append(current_design)def show_completed_models(completed_models):print("The following models have been printed:")for completed_model in completed_models:print(completed_model)unprinted_designs = ['phone case', 'robot pendant', 'dodecahedron'] completed_models = [] print_models(unprinted_designs, completed_models) show_completed_models(completed_models)#禁止函數(shù)修改列表 print_models(unprinted_designs[:], completed_models)傳遞任意數(shù)量的實(shí)參
#傳遞任意數(shù)量的實(shí)參數(shù) def make_pizza(*toppings):print(toppings)make_pizza('pepperoni') make_pizza('mushrooms', 'green peppers', 'extra cheese')#結(jié)合使用位置實(shí)參和任意數(shù)量實(shí)參 def make_pizza_size(size, *toppings):print(f"Making a {size} pizza with the following toppings:")for top in toppings:print(f"-{top}")make_pizza_size(16, 'pepperoni') make_pizza_size(12, 'mushrooms', 'green peppers', 'extra cheese')#使用任意數(shù)量的關(guān)鍵字實(shí)參數(shù) def build_profile(first, last, **user_info):user_info['first_name'] = firstuser_info['last_name'] = lastreturn user_infouser_info = build_profile('albert', 'einstein', location='princeton', field='physics') print(user_info)將函數(shù)存儲(chǔ)在模塊中
1、導(dǎo)入整個(gè)模塊
pizza.py
def make_pizza(size, *toppings):print(f"Making a {size} pizza with the following toppings:")for top in toppings:print(f"-{top}")pizza_main.py
import pizzapizza.make_pizza(16, 'pepperoni') pizza.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')2、導(dǎo)入特定的函數(shù)
#導(dǎo)入單個(gè)函數(shù) from module_name import function_name#導(dǎo)入多個(gè)函數(shù) from module_name import function_0, function_1 from pizza import make_pizzamake_pizza(16, 'pepperoni') make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')3、使用 as 給函數(shù)指定別名
from pizza import make_pizza as mpmp(16, 'pepperoni') mp(12, 'mushrooms', 'green peppers', 'extra cheese')4、使用 as 給模塊指定別名
import pizza as pp.make_pizza(16, 'pepperoni') p.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')函數(shù)編寫(xiě)指南
給形參指定默認(rèn)值時(shí),等號(hào)兩邊不要有空格:
def function_name(parameter_0, parameter_1='default value')對(duì)于函數(shù)調(diào)用中的關(guān)鍵實(shí)參,也遵循整個(gè)約定
function_name(value_0, parameter_1='value')函數(shù)參數(shù)縮進(jìn)對(duì)齊
def function_name(parameter_0, parameter_1, parameter_2,parameter_3, parameter_4, parameter_5):使用空行分隔多個(gè)函數(shù)
所有import 語(yǔ)句都放在文件開(kāi)頭
總結(jié)
以上是生活随笔為你收集整理的Python编程从入门到实践~函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 自动分页,返回时跳回指定页
- 下一篇: Python编程从入门到实践~操作列表~