Determine Number Divisibility in Python

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, we can use the modulo operator (%) to check if a number is divisible by another number. If the remainder is equal to 0, then the number is divisible. In this challenge, you will create a function that checks if a number is divisible by another number.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") subgraph Lab Skills python/comments -.-> lab-13668{{"`Determine Number Divisibility in Python`"}} python/booleans -.-> lab-13668{{"`Determine Number Divisibility in Python`"}} python/tuples -.-> lab-13668{{"`Determine Number Divisibility in Python`"}} python/function_definition -.-> lab-13668{{"`Determine Number Divisibility in Python`"}} end

Number is divisible

Write a function is_divisible(dividend, divisor) that takes two integers as arguments and returns True if the dividend is divisible by the divisor, and False otherwise.

def is_divisible(dividend, divisor):
  return dividend % divisor == 0
is_divisible(6, 3) ## True

Summary

In this challenge, you learned how to check if a number is divisible by another number using the modulo operator. You created a function that takes two integers as arguments and returns True if the dividend is divisible by the divisor, and False otherwise.

Other Python Tutorials you may like