Check if a number is divisible by another number in Python

Borislav Hadzhiev

Last updated: Jul 15, 2022

Check out my new book

**Use the modulo % operator to check if a number is divisible by another
number, e.g. if 9 % 3 == 0:. The modulo % operator returns the remainder
from the division of the first number by the second. If the remainder is 0,
the number is divisible by the other number.**

main.py

`if 9 % 3 == 0: print('number A is divisible by number B') if 15 % 5 == 0: print('number A is divisible by number B')`

We used the modulo `%`

operator to check if a number is divisible by another
number.

The modulo (%) operator returns the remainder from the division of the first value by the second.

main.py

`print(10 % 2) # 👉️ 0 print(10 % 4) # 👉️ 2`

If there is no remainder from the division, then the first number is an exact multiple of the second.

main.py

`print(16 % 4) # 👉️ 0 print(16 % 4 == 0) # 👉️ True`

`4`

is an exact multiple of `16`

, so `16`

is divisible by `4`

with a remainder
of `0`

.

**If you need to check if a number is not divisible by another number, use the
modulo % operator with the not equals != sign, e.g. if 15 % 4 != 0:.**

main.py

`print(15 % 4) # 👉️ 3 print(15 % 4 != 0) # 👉️ True if 15 % 4 != 0: print('15 is not divisible by 4')`

`15`

is not an exact multiple of `4`

, so dividing `15`

by `4`

gives us a
remainder of `3`

.

Here is an example that takes numbers from using input and checks if one number is divisible by another.

main.py

`num_1 = int(input('Enter a number: ')) print(num_1) # 👉️ 16 num_2 = int(input("Enter another number: ")) print(num_2) # 👉️ 4 if num_1 % num_2 == 0: print(f'{num_1} is divisible by {num_2}')`

The input function
takes an optional `prompt`

argument and writes it to standard output without a
trailing newline.

Notice that we used the `int()`

class to convert the input strings to integers.

The function then reads the line from input, converts it to a string and returns the result.

Even if the user enters a number, it still gets converted to a string.

If you need to check if a number is divisible by two or more other numbers, use
the `and`

operator.

main.py

`num = 15 if num % 3 == 0 and num % 5 == 0: # 👇️ this runs print('15 is divisible by 3 and 5')`

The expression `x and y`

returns the value to the left if it's falsy, otherwise
the value to the right is returned.

The `if`

block is only ran if both of the conditions evaluate to `True`

.

Conversely, if you need to check if a number is divisible by `1`

of multiple
numbers, use the `or`

operator.

main.py

`num = 15 if num % 10 == 0 or num % 5 == 0: # 👇️ this runs print('15 is divisible by 10 or 5')`

The expression `x or y`

returns the value to the left if it's truthy, otherwise
the value to the right is returned.

If either condition evaluates to `True`

, the `if`

block is ran.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

You can use the search field on my Home Page to filter through all of my articles.