python中的三元运算符

作者: Tim在路上 | 来源:发表于2018-10-04 20:14 被阅读1次

    三元运算符

    a if test else b
    

    如果test为真则返回a,否则返回b

    x = x+1 if x%2==1 else x
    

    实现斐波那契序列

    def fn(n):
        return n if n < 2 else fn(n-1)+fn(n-2)
    

    相关文章

      网友评论

        本文标题:python中的三元运算符

        本文链接:https://www.haomeiwen.com/subject/tgtfaftx.html