简简单单学Python

简简单单学Python,第1张

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

格式: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

返回字典:

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/943198.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-18
下一篇2022-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存