小编iot*_*taa的帖子

比较 if 语句中的多个值

与相同的几个元素相比,我将需要多个 if 语句,并且想知道我是否可以做一些事情来使代码更干净、更简单。

例子就是这个函数。

def test(num): 

    a = [1, 2, 3]

    if num == a : 
        return True

    else : 
        return False
Run Code Online (Sandbox Code Playgroud)

会回来

>>>test(1)
True
>>>test(2)
True
>>>test(5)
False
Run Code Online (Sandbox Code Playgroud)

1而不必为、2和编写单独的 if 语句3

python if-statement python-3.x

1
推荐指数
1
解决办法
6932
查看次数

标签 统计

if-statement ×1

python ×1

python-3.x ×1