美文网首页
2.分支与循环

2.分支与循环

作者: 恶魔缘 | 来源:发表于2018-05-21 23:02 被阅读0次

    2.分支与循环

    if else 语句简约写法

    elif ...:
    elif ...:
    else:
    ...
    

    1.条件表达式(三元操作符)

    if x < y:
        small = x
    else:
        small = y
    

    上述可写成:a = x if x<y else y

    2.assert断言

    常用于代码测试,在assert后语句条件为假,程序自动崩溃并抛出异常

    >>> assert 3>4
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AssertionError
    

    3.range

    for i in range(1,10,2):
        print(i)
    结果
    1
    3
    5
    7
    9
    

    range(a,b,c)

    • a 开始
    • b 结束
    • c 步长

    相关文章

      网友评论

          本文标题:2.分支与循环

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