函数
定义函数:
def user1():
print('Hello!')
user1()
注:unindent does not match any outer indentation level(未缩进不匹配任何外部缩进级别),表示缩进不正确,即:缩进4位,你只缩进3位就会出现此类报错
#向函数传递信息
def user1(name):
print(name.title()+'Hello!')
user1('cx')
注:此过程中,’cx’是实参,name是形参
- 1.位置实参
def user1(name,fruit):
print(name.title()+' like eat:'+fruit)
user1('cx','apple')
- 2.关键字实参
关键字实参是传递给函数的名称-值对,不会受位置的影响
def user1(name,fruit):
print(name.title()+' like eat:'+fruit)
user1(fruit='apple',name='cx')
- 3.默认值
def user1(name,fruit='bnana'):
print(name.title()+' like eat:'+fruit)
user1(fruit='apple',name='cx')
user1(name='cc')
user1('ccc')
user1('cx','apple')
注:第一种是修改了默认值,并且将名字赋值,两者位置不需与形参位置对应
第二种第三种陡是使用默认值,为名字赋值,但是视为位置参数,需要将未设默认值的形参放在前面,因为系统默认与第一个匹配
第四种:设置了默认值也同样可以使用位置关系来实现实参传递
- 返回值
def user1(name,fruit='bnana'):
like=(name.title()+' like eat:'+fruit)
return like
info=user1('cc')
print(info)
- 返回字典
def user1(name1,fruit1='bnana'):
like={'name':name1,'fruit':fruit1}
return like
info=user1('cc')
print(info)
- 可选参数
def user1(name1,fruit1,fruit2=''):
like={'name':name1,'fruit':fruit1}
if fruit2:
like['fruit2']=fruit2
return like
info=user1('cc','banana','apple')
print(info)
- 传递列表
def user1(info):
for user in info:
print('Hello '+user)
info=['cc','cx','xc']
user1(info)
- 在函数中修改列表
animal=['cat','tiger','snake']
releases=[]
def r_print(animal1,releases1):
while animal1:
get=animal1.pop()
releases1.append(get)
def show(releases2):
for release3 in releases2:
print(release3)
r_print(animal,releases)
show(releases)
- 禁止函数修改列表
即不修改原来列表中的数据而达到数据的变换,此时候就需要使用切片
animal=['cat','tiger','snake']
releases=[]
def r_print(animal1,releases1):
while animal1:
get=animal1.pop()
releases1.append(get)
def show(releases2):
for release3 in releases2:
print(release3)
r_print(animal[:],releases)#切片
show(releases)
show(animal)
- 传递任意数量的实参
#传递任意数量的实参
def animal(*animals):
for animal in animals:
print(animal)
animal('cat')
animal('snake','tiger','dog')
- 结合使用位置参数和任意数量实参
如果要让函数接收不同类型的实参,必须在函数定义中将接纳任意数量实参的形参放在最后。Python先匹配位置实参和关键字实参,再将余下的实参都收集到最后一个形参中
def animal(size,*animals):
for animal in animals:
print(size,animal)
animal(16,'cat')
animal(17,'snake','tiger','dog')
- 使用任意数量的关键字实参
def users_info(first,last,**user_info):
profile={}
profile['first_name']=first
profile['last_name']=last
for key,value in user_info.items():
profile[key]=value
return profile
profile=users_info('c','x',location='hubei',field='physics')
for key,value in profile.items():
print(key,value)
形参**user_info中的两个星号让Python创建一个名为user_info的空字典,并将收到的名称—值对都封装到这个字典中
将函数存储在模块中
函数的优点之一就是:他们可以与主程序分离
- 导入整个模块
我们将
def users_info(first,last,**user_info):
profile={}
profile['first_name']=first
profile['last_name']=last
for key,value in user_info.items():
profile[key]=value
return profile
单独新建一个.py文件,命名为user_firstlast_info.py
然后另外创建一个文件test.py
import user_firstlast_info
profile= user_firstlast_info.users_info('c','x',location='hubei',field='physics')
for key,value in profile.items():
print(key,value)
即可完成测试
- 导入特定的函数
from user_firstlast_info import function_name
function_name是该模块中的函数名,因为可能一个模块中有多个函数,但是你只想用其中的一个,这样就不必导入整个模块了。
只导入一个方法时,就可以直接用该方法而不需要在前面写模块名
即:
From user_firstlast_info import users_info
profile=users_info('c','x',location='hubei',field='physics')
导入模块中多个函数时,只需要用逗号分隔开即可,
from user_firstlast_info import function_1, function_2, function_3
也可以直接使用函数名来访问
使用as给模块指定别名
import user_firstlast_info as u
profile= u.users_info('c','x',location='hubei',field='physics')
导入模块中所有的函数
import user_firstlast_info *
可以直接使用函数名来访问而前面不加模块名,但是此方法不建议使用,因为可能函数名与本项目中已有的函数同名,所以建议要么只导入你需要用的函数,要么就导入模块