AI智能
改变未来

python进阶(5)–函数

文档目录:

一、函数体
二、实参与形参
三、返回值
四、举例:函数+while循环
五、举例:列表/元组/字典传递
六、模块与函数的导入

—————————————分割线:正文——————————————————–

一、函数体

1、定义删除

def green_user():\"\"\"显示简单的问候语\"\"\"print(\"hello world!\")green_user()

查看结果:

hello world!

2、函数传递信息

def green_user(username):\"\"\"显示简单的问候语\"\"\"print( f\"Hello,{username.title()}!\" )green_user(\"Lucy\")

查看结果:

Hello,Lucy!

二、实参与形参

1、概念

def green_user(username):\"\"\"显示简单的问候语\"\"\"print( f\"Hello,{username.title()}!\" )green_user(\"Lucy\")

形参:username

实参:\”Lucy\”

2、位置实参,且支持多次调用

def describe_pet(animal_type,pet_name):\"\"\"显示宠物的信息\"\"\"print(f\"I have a {animal_type}\")print(f\"My {animal_type}\'s name is {pet_name}\")describe_pet(\'dog\',\'Mary\')describe_pet(\'cat\',\'Little White\')

查看结果:

I have a dogMy dog\'s name is MaryI have a catMy cat\'s name is Little White

3、实参中将名称和值关联起来

def describe_pet(animal_type,pet_name):\"\"\"显示宠物的信息\"\"\"print(f\"I have a {animal_type}\")print(f\"My {animal_type}\'s name is {pet_name}\")describe_pet(animal_type=\'dog\',pet_name=\'Mary\')

查看结果:

I have a dogMy dog\'s name is Mary

4、默认值,需放在形参最后

def describe_pet(pet_name,animal_type=\'cat\'):\"\"\"显示宠物的信息\"\"\"print(f\"I have a {animal_type}\")print(f\"My {animal_type}\'s name is {pet_name}\")describe_pet(pet_name=\'Mary\')describe_pet(\'Big White\')

查看结果:

I have a catMy cat\'s name is MaryI have a catMy cat\'s name is Big White

三、返回值

1、返回简直值:return

def get_formatted_name(first_name,last_name):\"\"\"返回全名,并且首字母大写\"\"\"full_name=f\"{first_name} {last_name}\"return full_name.title()name=get_formatted_name(\"mike\",\"jackson\")print(name)

查看结果:

Mike Jackson

2、实参可选

def get_formatted_name(first_name,last_name,middle_name=\'\'):\"\"\"返回全名,并且首字母大写\"\"\"full_name=f\"{first_name} {middle_name} {last_name}\"return full_name.title()name1=get_formatted_name(\"mike\",\"jackson\",\'midddd\')name2=get_formatted_name(\"mike\",\"jackson\")print(name1)print(name2)

3、返回字典

def build_person(first_name,last_name):\"\"\"返回一个字段\"\"\"persion={\'first\':first_name,\'last\':last_name}return persiondicttest1=build_person(\'xiao\',\'long\')print(dicttest1)

查看结果:

{\'first\': \'xiao\', \'last\': \'long\'}

四、举例:函数+while循环

def get_formatted_name(first_name,last_name):\"\"\"返回全名,并且首字母大写\"\"\"full_name=f\"{first_name} {last_name}\"return full_name.title()while True:print(\"Please tell me your name:\")print(\"(enter \'q\' at any time to quit)\")f_name=input(\"First name:\")if f_name==\'q\':breakl_name=input(\"Last name:\")if l_name==\'q\':breakformatted_name=get_formatted_name(\'mike\',\'jackson\')print(f\"\\nhello,{formatted_name}!\")

查看结果:

Please tell me your name:(enter \'q\' at any time to quit)First name:mikeLast name:jacksonhello,Mike Jackson!Please tell me your name:(enter \'q\' at any time to quit)First name:tomLast name:q

五、举例:列表/元组/字典传递

1、传递列表

def greet_users(names):\"\"\"向列表中的每位用户发出简单的问候\"\"\"for name in names:msg=f\"Hello,{name.title()}!\"print(msg)username=[\'bk\',\'anna\',\'tom\',\'mary\']greet_users(username)

查看结果:

Hello,Bk!Hello,Anna!Hello,Tom!Hello,Mary!

2、在函数中修改列表

unconfirmed_users=[\'alice\',\'brian\',\'candace\']confirmed_users=[]def confirmed(unconfirmed_users,confirmed_users):while unconfirmed_users:current_user = unconfirmed_users.pop()print( f\"Verfying users:{current_user.title()}\" )confirmed_users.append( current_user )# 显示所有验证的用户def print_confirmed_users(confirmed_users):print( \"\\nThe following users have been confirmed!\" )for confirm_user in confirmed_users:print( confirm_user.title() )confirmed(unconfirmed_users,confirmed_users)print_confirmed_users(confirmed_users)print(unconfirmed_users)

查看结果:

Verfying users:CandaceVerfying users:BrianVerfying users:AliceThe following users have been confirmed!CandaceBrianAlice[]

3、优化:函数修改原列表

#将列表的副本传递给函数unconfirmed_users=[\'alice\',\'brian\',\'candace\']confirmed_users=[]def confirmed(unconfirmed_users,confirmed_users):while unconfirmed_users:current_user = unconfirmed_users.pop()print( f\"Verfying users:{current_user.title()}\" )confirmed_users.append( current_user )# 显示所有验证的用户def print_confirmed_users(confirmed_users):print( \"\\nThe following users have been confirmed!\" )for confirm_user in confirmed_users:print( confirm_user.title() )confirmed(unconfirmed_users[:],confirmed_users)print_confirmed_users(confirmed_users)print(unconfirmed_users)

查看结果:原列56c表不会修改

Verfying users:CandaceVerfying users:BrianVerfying users:AliceThe following users have been confirmed!CandaceBrianAlice[\'alice\', \'brian\', \'candace\']

4、传递任意数量的实参(元组)

#*号可以生成一个空元组,封装接收的所有值def make_pizza(*toppings):\"\"\"打印顾客点的所有配料\"\"\"print(toppings)make_pizza(\'pepperoni\')make_pizza(\'mushrooms\',\'chesse\',\'green peppers\')

查看结果:

(\'pepperoni\',)(\'mushrooms\', \'chesse\', \'green peppers\')

5、结合使用位置实参和任意数量实参

def make_pizza(size,*toppings):\"\"\"打印顾客点的所有配料\"\"\"print(f\"Making a {size}-inch pizza with the folowing toppings\")for topping in toppings:print(f\'-{topping}\')make_pizza(16,\'pepperoni\')make_pizza(12,\'mushrooms\',\'chesse\',\'green peppers\')

查看结果:

Making a 16-inch pizza with the folowing toppings-pepperoniMaking a 12-inch pizza with the folowing toppings-mushrooms-chesse-green peppers

6、使用任意数量的关键字实参(字典)

#**可以生成一个空字典def build_profile(first,last,**user_info):\"\"\"创建一个字典,其中包含有关用户的信息\"\"\"user_info[\'first_name\']=firstuser_info[\'last_name\']=lastreturn user_infouser_profile=build_profile(\'albert\',\'master\',location=\'princeton\',field=\'physics\',number=1)print(user_profile)

查看运行结果:

{\'location\': \'princeton\', \'field\': \'physics\', \'number\': 1, \'first_name\': \'albert\', \'last_name\': \'master\'}

六、模块与函数的导入

1、导入模块

import test04_functiontest04_function.make_pizza(13,\'prpperono\')test04_function.make_pizza(10,\'prpperono\',\'cheese\',\'mushrooms\')

查看结果:

Making a 13-inch pizza with the folowing toppings-prpperonoMaking a 10-inch pizza with the folowing toppings-prpperono-cheese-mushrooms

2、导入特定的函数

from test04_function import make_pizzamake_pizza(10,\'prpperono\',\'cheese\',\'mushrooms\')

3、使用as给函数指定别名

from test04_function import make_pizza as mpmp(10,\'prpperono\',\'cheese\',\'mushrooms\')

4、使用as给模块执行别名

import test04_function as pizzapizza.make_pizza(10,\'prpper56cono\',\'cheese\',\'mushrooms\')

5、导入模块中的所有函数

from test04_function import *

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » python进阶(5)–函数