naz
naz

Reputation: 2072

Iterate through integers to find mod in if statement

How do I make the below code neater

if integer % 2 == 0 or integer % 3 == 0 or ... integer % 9 == 0:

edit:

I am trying to check whether the variable integer is fully (i.e. there is no remainder after the division) divisible by any of the following integers: [2,3,4,5,6,7,8,9]. And I was wondering whether there is a better way to write that if statement.

Upvotes: 1

Views: 95

Answers (1)

Yu Hao
Yu Hao

Reputation: 122403

One way is to use any:

if any(integer % x == 0 for x in range(2, 10)):

Upvotes: 5

Related Questions