日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python编程从入门到实践~函数

發布時間:2024/7/23 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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编程从入门到实践~函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。