简简单单学Python

函数:带名字的一个代码块,用于完成具体的工作 ,要执行函数定义的特定的任务,可调用函数

格式:def 函数名():

def namefunc(username):    ## username 是形参
	print("Your friend is "+username.title()+"")
namefunc("Mike")

Your friend is Mike        ## Mike 是实参

传递实参:

①位置实参(顺序位置不能乱)

def mypet(animal,name):
	print("\nI have a "+animal+"")
	print("My "+animal+"'s name is "+name+" .")
mypet("dog","duoduo")    ## 顺序很重要

I have a dog
My dog's name is duoduo .

②关键字实参(不关心位置顺序)

def mypet(animal,name):
	print("\nI have a "+animal+"")
	print("My "+animal+"'s name is "+name+" .")
#mypet("dog","duoduo")
mypet(animal="dog",name="duoduo")  ## 实参顺序/位置可以改变
mypet(name="duoduo",animal="dog")  ## 只要关键字能对应

I have a dog
My dog's name is duoduo .

I have a dog
My dog's name is duoduo .

默认值:(形参可以先指定默认值)

def mypet(name,animal='dog'):   ## 只能对后面的形参设默认值
	print("\nI have a "+animal+"")
	print("My "+animal+"'s name is "+name+" .")
mypet("duoduooo")               ##两种格式
mypet(name="duoduo")

I have a dog
My dog's name is duoduooo .

I have a dog
My dog's name is duoduo .


返回值;return将值返回到调用函数的代码行

def mypet(animal,anima2):
	out=("\nI have two dogs,its name are "+animal+" and "+anima2+",respectly.")
	return print(out)
mypet("duoduooo","didi")

I have two dogs,its name are duoduooo and didi,respectly.
def dogname(A,B,C):
	name=A+' '+B+' '+C
	return name.title()
dogs=dogname('duoduo','didi','haha')
print(dogs)

Duoduo Didi Haha

返回字典:

今天就在这里吧 学不进去了。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值