Check if a Number is Even

PythonPythonBeginner
Practice Now

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

Introduction

In Python, we can easily check if a number is even or odd. In this challenge, you will be tasked with writing a function that checks whether a given number is even or not.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") subgraph Lab Skills python/comments -.-> lab-13670{{"`Check if a Number is Even`"}} python/booleans -.-> lab-13670{{"`Check if a Number is Even`"}} python/function_definition -.-> lab-13670{{"`Check if a Number is Even`"}} end

Check if a Number is Even

Write a function is_even(num) that takes in a number as an argument and returns True if the number is even and False if the number is odd. To check whether a number is even or odd, you can use the modulo (%) operator. If a number is even, it will have no remainder when divided by 2. If a number is odd, it will have a remainder of 1 when divided by 2.

def is_even(num):
  return num % 2 == 0
is_even(3) ## False

Summary

In this challenge, you learned how to check if a number is even or odd in Python using the modulo operator. You can use this function to check whether a number is even or odd in your own programs.

Other Python Tutorials you may like