Owen Grimm
Owen Grimm

Reputation: 3

Check a number's modulus from every number in a list in a if statement

I'm trying to make a program that involves checking if a number's modulus is zero for every number in a list, and then, if it's modulus of all of them is zero, it adds it to the list. Like this:

nums = [1,2,3,5]
if( var1% *Everything in nums* ==0):
    nums.append(var1)

If you're wondering, It's for calculating primes. Thanks in advance.

Upvotes: 0

Views: 52

Answers (1)

khelwood
khelwood

Reputation: 59111

There is a function called all for this kind of thing.

nums = [1,2,3,5]
if all(var1%n==0 for n in nums):
    nums.append(var1)

Upvotes: 3

Related Questions