/ 定义函数
def 函数名:
函数体
函数调用
def hanshu(): #定义函数
print('Hello World !')
print('Hello World !')
hanshu()
/ 向函数传递信息
def hanshu(name): #name是形参
print(f'Hello {name} !')
hanshu('joker') #joker是实参
/ 传递实参
1.
def hanshu(name,age):
print(f'Hello {name} !')
print(f'You are {age} now.')
hanshu('joker','18')
2.默认值
def hanshu(name,age = "18"):
print(f'Hello {name} !')
print(f'You are {age} now.')
hanshu('joker')
def hanshu(name,age = "18"):
print(f'Hello {name} !')
print(f'You are {age} now.')
hanshu('joker','20')
3.关键字
def hanshu(name,age = "18"):
print(f'Hello {name} !')
print(f'You are {age} now.')
hanshu(age = '20',name='joker')
此时,与实参的顺序无关
/ 返回值
def hanshu(name,age):
information = f'the name is {name} \nthe age is {age}'
return information #返回
infor = hanshu("joker","18")
print(infor)
/ 实参可选
def hanshu(name,age,others=''):
if others:
information =f'{name} {age} {others}'
else:
information= f'{name} {age}'
return information #返回值
infor = hanshu("joker","18",'1774585')
print(infor)
infor = hanshu("joker","18")
print(infor)
/ 返回一个字典
def dictionary(green,two,ten = None):
person={'color': green,
'height':two}
if ten:
person['ten']=ten
return dictionary
dic = {'red','ten','two'}
print(dic)
/ 使用 as 命名