Python编程从入门到实践~函数
生活随笔
收集整理的這篇文章主要介紹了
Python编程从入门到实践~函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
定義函數
#定義函數 def greet_user():print("Hello~")#調用函數 greet_user()#向函數傳遞信息 def print_user(username):print(f"Hello~{username}")print_user('Jesse')傳遞實參數
#位置實參 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')#關鍵字實參數&默認值 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')返回值
#返回簡單值 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)#讓實參數可選 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)傳遞列表
#在函數中修改列表 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)#禁止函數修改列表 print_models(unprinted_designs[:], completed_models)傳遞任意數量的實參
#傳遞任意數量的實參數 def make_pizza(*toppings):print(toppings)make_pizza('pepperoni') make_pizza('mushrooms', 'green peppers', 'extra cheese')#結合使用位置實參和任意數量實參 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')#使用任意數量的關鍵字實參數 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)將函數存儲在模塊中
1、導入整個模塊
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、導入特定的函數
#導入單個函數 from module_name import function_name#導入多個函數 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 給函數指定別名
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')函數編寫指南
給形參指定默認值時,等號兩邊不要有空格:
def function_name(parameter_0, parameter_1='default value')對于函數調用中的關鍵實參,也遵循整個約定
function_name(value_0, parameter_1='value')函數參數縮進對齊
def function_name(parameter_0, parameter_1, parameter_2,parameter_3, parameter_4, parameter_5):使用空行分隔多個函數
所有import 語句都放在文件開頭
總結
以上是生活随笔為你收集整理的Python编程从入门到实践~函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自动分页,返回时跳回指定页
- 下一篇: 或者是修改服务器时间,修改云服务器时间设