我的代码看起来像这样:
if x == 1 or x == 2 or x == 3 or x == 4 or x == 5 or x == 6:
print('Text')
所以我想知道我是否可以将所有这些条件组合成一个列表,比如:
if x == {1,2,3,4,5,6}:
print('Text')
让我的代码更干净,而且对我来说工作更少。
谢谢。
请您参考如下方法:
您可以使用 in 运算符和一组值代替:
if x in {1, 2, 3, 4, 5, 6}:
print('Text')
