目录
1.定义一个函数
2. 实参形参
3.传递实参
3.1 位置实参
3.2 关键字实参
3.3 返回字典
3.4 传递列表
3.5 传递任意实参,用元组的方法
3.6 字典传递任意数量的实参
4.将函数存储在模块中
4.1 导入整个模块
4.2 导入特定函数
4.3 使用 as 给函数 / 模块指定别名
1.定义一个函数
格式: def function_name(option)
statements
def print_hi(name):
print(f'Hi, {name}')
print_hi('PyCharm')
输出: Hi, PyCharm
2. 实参形参
def print_hi(name) 中,name 就是形参,函数完成其工作所需的一项信息。
print_hi('PyCharm')中,PyCharm 是一个实参,实参是调用函数时传递给函数的信息。
3.传递实参
传递实参可以使用位置实参、关键字实参、列表或字典。
3.1 位置实参
要求实参顺序和形参顺序相同
3.2 关键字实参
关键字实参传递给函数的名称--值对。
def food(f_type,f_name):
print("\nI have " + f_name + ".")
print("The " + f_type + "'s is " + f_name.title())
food('fruit','apple') #传递实参,要求实参顺序和形参顺序相同
food(f_type = 'fruit',f_name = 'apple') #关键字实参传递给函数的名称--值对
def f_name(first_name,last_name,mid_name = ''): #给形参设定默认值
if mid_name:
full_name = first_name + '' + mid_name + '' + last_name
else:
full_name = first_name + '' + last_name
return full_name.title()
print(f_name('anna','black')) #函数调用时没有指定实参,将会调用默认值
print(f_name('Soft','black','evel'))
输出:
I have apple.
The fruit's is Apple
Annablack
Softevelblack
3.3 返回字典
def f_name(first_name,last_name,mid_name=''):
full_name = {'frist':first_name,'last':last_name,'mid':mid_name}
return full_name
print(f_name('dfs','ffw','htrg'))
print(f_name('sdf','htr'))
{'frist': 'dfs', 'last': 'ffw', 'mid': 'htrg'}
{'frist': 'sdf', 'last': 'htr', 'mid': ''}
3.4 传递列表
def print_model(print_design,completed_model):
while print_design:
cur_design = print_design.pop()
print("Print model: " + cur_design)
completed_model.append(cur_design)
def show_completed_model(completed_model):
print("The models have been printed: ")
for com_model in completed_model:
print(com_model)
print_design = ['iphone case','reboot','dodecahe']
completed_model = []
print_model(print_design[:],completed_model) # print_design[:] 表示 print_design的备份,
# print_model函数对备份的操作不会影响原来的列表
print('\n')
show_completed_model(completed_model)
show_completed_model(print_design)
Print model: dodecahe
Print model: reboot
Print model: iphone case
The models have been printed:
dodecahe
reboot
iphone case
The models have been printed:
iphone case
reboot
dodecahe
3.5 传递任意实参,用元组的方法
def food(*tos): # * 让Python创建了一个名为 tos 的空元组,并将接收到的所有值都装到元组中
if tos.__len__() != 1 :
for to in tos:
print(to + '-',end='') # end = '' Python的不换行输出
else:
print(tos) # end = '' Python的不换行输出
food('fwe')
food('fsfew','213','房购房')
('fwe',)
fsfew-213-房购房-
def food(size,*tos): # * 让Python创建了一个名为 tos 的空元组,并将接收到的所有值都装到元组中
if tos.__len__() != 1 :
for to in tos:
print(size,to )
else:
print(size,tos)
food(12,'fwe')
food(45,'fsfew','213','房购房')
12 ('fwe',)
45 fsfew
45 213
45 房购房
3.6 字典传递任意数量的实参
def f_name(frist,last,**user_info): # ** 是让Python创建一个名为 user_info 的空字典,
# 并将接收到的键值对放入其中
profile = {}
profile['frist_name'] = frist
profile['last_name'] = last
for key,value in user_info.items(): # .items() 返回的是键值对
profile[key] = value
return profile
user_profile = f_name('albert','einstein',location = 'princeton',field = 'physics')
print(user_profile)
{'frist_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}
4.将函数存储在模块中
4.1 导入整个模块
什么是模块?
模块是扩展名为 .py 的文件,包含要导入程序中的代码
在 main.py 中加入代码行 import add 让Python打开 add.py 文件,那么在 main.py 文件中就可以使用句点通过名称来调用 add.py 中的文件了
4.2 导入特定函数
无需使用句点,可以直接通过名称来调用函数,导入语法:
from module_name import func_name1, func_name2, func_name3
4.3 使用 as 给函数 / 模块指定别名
当函数名太长或者与当前的模块中的函数有冲突,可以用 as 函数取别名
指定函数:
from module_name import func_name as fn
指定模块:
import module_name as mn
4.4 导入模块中所以函数
语法:
from module_name import *
通配符 * 号匹配所有函数,无需使用句点,可以直接通过名称来调用函数。
不推荐使用 * ,在Python中可能遇到多个名称相同的函数和变量,进而覆盖函数,而不是分别导入所有的函数,最佳做法是只导入所需要的函数,或者导入模块使用句点表示法,这能使代码更清晰,更容易阅读和理解。