基础(1)创建一个函数并定于注释和默认值

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(函数名)就可以

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值