• 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
  • VBA
  • About
You are here: Home / Python / How to Check if Number is Divisible by 3 in Python

How to Check if Number is Divisible by 3 in Python

February 7, 2022 Leave a Comment

In Python, we can check if a number is divisible by 3 very easily with the Python built in remainder operator %. If the remainder of a number after dividing by 3 is 0, then the number is divisible by 3.

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

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

#Output:
False
True

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

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

To determine if a number is divisible by 3 using Python, we divide by 3. If the remainder after division is 0, then the number is the number is divisible by 3. If it is not 0, then the number is not divisible by 3.

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

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

In Python, we can 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

How to Determine 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, if we want to determine if a number is divisible by 5, just put 5 after the % operator.

def isDivisibleBy5(num):
    if (num % 5) == 0:
        return True
    else:
        return False

print(isDivisibleBy5(10))
print(isDivisibleBy5(15))

#Output:
True
True

Hopefully this article has been useful for you to understand how to determine if a number is divisible by 3 in Python.

Other Articles You'll Also Like:

  • 1.  Factorial Program in Python Using For Loop and While Loop
  • 2.  Python dirname – Get Directory Name of Path using os.path.dirname()
  • 3.  Count Number of Keys in Dictionary in Python
  • 4.  How to Split List in Half Using Python
  • 5.  How to Check if a String Contains Vowels in Python
  • 6.  Check if a Number is Divisible by 2 in Python
  • 7.  Are Dictionaries Mutable in Python? Yes, Dictionaries are Mutable
  • 8.  Get Random Letter in Python
  • 9.  Convert First Letter of String to Lowercase in Python
  • 10.  Using Python to Print Degree Symbol

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy