• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Check if Number is Divisible by Another Number

Using Python to Check if Number is Divisible by Another Number

May 8, 2022 Leave a Comment

To check if a number is divisible by another number, you can use the Python built in remainder operator %. If the remainder after division is 0, then the number is divisible by the number you divided by.

def divisible_by(x, y):
    if (x % y) == 0:
        return True
    else: 
        return False

print(divisible_by(10,2))
print(divisible_by(15,6))

#Output:
True
False

When working with numbers in Python, it can be useful to know if the numbers you are working with are divisible by certain numbers.

We can use the Python built in remainder operator % to get the remainder of a number after division.

If the remainder after division is 0, then the number is divisible by the number you divided by.

Below is an example of a function which will check if a number is divisible by another number in Python.

def divisible_by(x, y):
    if (x % y) == 0:
        return True
    else: 
        return False

print(divisible_by(10,2))
print(divisible_by(15,6))

#Output:
True
False

How to Check if a Number is Divisible by Another Number

Using the Python remainder operator %, we can determine if a number is divisible by any other number.

For example, to check if a number is divisible by 2 using Python, we divide by 2. If the remainder after division is 0, then the number is the number is divisible by 2. If it is not 0, then the number is not divisible by 2.

Below is a function which will check if a number is divisible by 2 in Python.

def isDivisibleBy2(num):
    if (num % 2) == 0:
        return True
    else:
        return False

print(isDivisibleBy2(10))
print(isDivisibleBy2(15))

#Output:
True
False

If we want to check if a number is divisible by 3, just put 3 after the % operator.

def isDivisibleBy3(num):
    if (num % 3 == 0:
        return True
    else:
        return False

print(isDivisibleBy3(10))
print(isDivisibleBy3(15))

#Output:
False
True

How to Check if a Number is Even or Odd Using Python

We can also use % to check if a number is even or odd very easily with the Python built in remainder operator %. If the remainder of a number after dividing by 2 is 0, then the number is even. If not, the number is odd.

def isEven(num):
    if (num % 2) == 0:
        return True
    else:
        return False

print(isEven(10))
print(isEven(15))

#Output:
True
False

Hopefully this article has been useful for you to learn how to check if a number is divisible by another number in Python.

Other Articles You'll Also Like:

  • 1.  How to Group and Aggregate By Multiple Columns in Pandas
  • 2.  Using Python to Replace Multiple Spaces with One Space in String
  • 3.  How to Remove Vowels from a String in Python
  • 4.  How to Get the Size of a List in Python Using len() Function
  • 5.  Using Python to Find Minimum Value in List
  • 6.  Create Empty List in Python
  • 7.  Get Day of Week from Datetime in pandas DataFrame
  • 8.  Calculating Sum of Squares in Python
  • 9.  nunique pandas – Get Number of Unique Values in DataFrame
  • 10.  Using Python to Iterate Over Two Lists

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy