Check if a Number is Even

Beginner

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.

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.