【发布时间】:2015-04-01 00:07:17
【问题描述】:
我将需要多个 if 语句来比较相同的几个元素,并且想知道我是否可以按照这些思路做一些事情来使代码更简洁。
例如这个函数。
def test(num):
a = [1, 2, 3]
if num == a :
return True
else :
return False
会回来
>>>test(1)
True
>>>test(2)
True
>>>test(5)
False
不必为1、2 和3 编写单独的 if 语句。
【问题讨论】:
标签: python if-statement python-3.x