Python日记(5)

函数

定义函数

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  *

可以直接使用函数名来访问而前面不加模块名,但是此方法不建议使用,因为可能函数名与本项目中已有的函数同名,所以建议要么只导入你需要用的函数,要么就导入模块

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈行恩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值