Exercise_8

Exercise 8 : Branch and Leap 1

Test :

  1. if not (money < 100) : if money >= 100

  2. assert condition(False) #AssertionError

  3. 交换 a=1, b=2,c=3

    a,b,c = c,a,b

  4. (x < y and [x] or [y])[0]

    实现三元操作符功能 x if condition else y

  5. 成员资格运算符 : in 如果在序列中返回True,否则False

Try :

  1. 将以下代码使用三元操作符实现

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    x, y, z = 6, 5, 4
    if x < y:
    small = x
    if z < small:
    small = z
    elif y < z:
    small = y
    else:
    small = z
    可以改成:
    small = x if(x < y and x < z) else (y if y< z else z)

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!