有没有办法让这段代码不那么可怕? Python 3

Is there a way to make this code less horrendous? Python 3

所以我试图让一个数字同时除以 1 到 7。 如何简化 "if" 部分?

我是初学者,所以如果可能的话,让它简单易懂。

谢谢!

result = []

for _ in range(1, 9999):
    if _ % 1 == 0 and _ % 2 == 0 and _ % 3 == 0 and _ % 4 == 0 and _ % 5 == 0 and _ % 6 == 0 and _ % 7 == 0:
        result.append(_)

print(result)

如果你使用一个变量,你不应该将它命名为 _,这个字符用于需要分配但从未使用过的东西。最后,您正在寻找我认为的 all(...) 语法:

result = []

for num in range(1, 9999):
    if all(num % x == 0 for x in range(1, 8)):
        result.append(num)

print(result)

或一行:

result = [num for num in range(1, 9999) if all(num % x == 0 for x in range(1, 8))]