Course Content
Python Indentation, Comments and Variables
0/2
Object Oriented Programming in Python
0/1
Exception Handling in Python
0/1
Sending emails with Python
0/1
Unit test in python programming
0/1
Python programming (zero to advance!!!)
About Lesson

Divisible by 3

Define a function named div_3 that returns True if its single integer parameter is divisible by 3 and False otherwise.

For example, div_3(6) is True because 6/3 does not leave any remainder. However div_3(5) is False because 5/3 leaves 2 as a remainder.

 

==== SOLUTION ====

You can implement the `div_3` function in Python by checking if the given integer is divisible by 3 without leaving any remainder. Here’s how you can do it:

“`python
def div_3(n):
      return n % 3 == 0

# Test the function
print(div_3(6)) # Output: True
print(div_3(5)) # Output: False
“`

Explanation:

1. `def div_3(n):`: This line defines a function named `div_3` that takes a single integer parameter `n`.

2. `return n % 3 == 0`: This line returns `True` if `n` is divisible by 3 without leaving any remainder (i.e., the result of `n % 3` is 0), and `False` otherwise.

So, when you call `div_3(6)`, it returns `True` because 6 is divisible by 3 without leaving any remainder. Similarly, when you call `div_3(5)`, it returns `False` because 5 is not divisible by 3 without leaving a remainder.

 

Join the conversation