1、创建一个函数
def chenfun1():
print('test chen1')
注意:记得函数的开头空两格!
2、创建一个带参数的函数并注释
def chenfun1():
print('test chen1')
def chenfun2(wordshow):
"""show the input word"""
print(wordshow)
注意:(1)注释整段文字, docstring,建议用6个双引号"""XXX"""!(2)变量字符串建议用两个单引号''(3)如果是\则用双引号"\"
3、 包含注释的函数
def chenfun1():
print('test chen1')
def chenfun2(wordshow):
"""show the input word"""
print(wordshow)
def chenfun3(wordshow:str)->str:
"""show the input word include input type and return type"""
print(wordshow)
return wordshow
注意:(1)参数注释是在参数后面加了:和类型(2)返回注释是函数最后加了->和类型;(3)类型根据自己的习惯写,编译器不管你,那是给你和你的同事看的
4、添加函数的默认值
def chenfun1():
print('test chen1')
def chenfun2(wordshow):
"""show the input word"""
print(wordshow)
def chenfun3(wordshow:str)->str:
"""show the input word include input type and return type"""
print(wordshow)
return wordshow
def chenfun4(wordshow:str='chendefault')->str:
"""show the input word include input type and return type"""
print(wordshow)
return wordshow
此时如果输入为空的话就调用默认值,如果有输入的话,就是输入
5、 查看函数和注释
在IDE中输入help(函数名)就可以