How to Check if a Number is Even or Odd Using Python: A Step-by-Step Guide

In the world of programming, determining whether a number is even or odd is a fundamental task. This essential concept is a stepping stone to more complex programming challenges and is crucial for beginners. In this guide, we will explore how to create a Python program that checks if a given number is even or odd. This step-by-step tutorial is designed to be accessible for beginners, providing clear explanations and practical examples.

What are Even and Odd Numbers?

Before diving into the code, let’s briefly review what even and odd numbers are:

  • Even Numbers: An even number is any integer that can be divided by 2 without leaving a remainder. Examples include -4, 0, 2, 8, 10, …
  • Odd Numbers: An odd number is any integer that, when divided by 2, leaves a remainder of 1. Examples include -3, 1, 5, 9, 11, …

Understanding the difference between even and odd numbers is crucial for following this tutorial.

Writing the Python Program to Check if a Number is Even or Odd

Method 1: Using Modulus Operator

This is the most common and straightforward method.

def check_even_odd(number):
    if number % 2 == 0:
        return "Even"
    else:
        return "Odd"

number = int(input("Enter a number: "))
result = check_even_odd(number)
print(f"The number {number} is {result}.")

Here is the screenshot of the python program and its output.
Check even or odd-Using Modulus Operator

Method 2: Using Bitwise AND Operator

This method uses the bitwise AND operator to determine if a number is even or odd.

def check_even_odd(number):
    if number & 1 == 0:
        return "Even"
    else:
        return "Odd"

number = int(input("Enter a number: "))
result = check_even_odd(number)
print(f"The number {number} is {result}.")

Check even or odd-Using Bitwise Operator

Method 3: Using Divmod Function

The divmod() function returns a tuple containing the quotient and the remainder when dividing two numbers.

def check_even_odd(number):
    if divmod(number, 2)[1] == 0:
        return "Even"
    else:
        return "Odd"

number = int(input("Enter a number: "))
result = check_even_odd(number)
print(f"The number {number} is {result}.")

Method 4: Using String Formatting

This method uses string formatting to simplify the output.

def check_even_odd(number):
    return "Even" if number % 2 == 0 else "Odd"

number = int(input("Enter a number: "))
result = check_even_odd(number)
print(f"The number {number} is {result}.")

Method 5: Using Lambda Function

This method uses a lambda function to check if a number is even or odd.

check_even_odd = lambda number: "Even" if number % 2 == 0 else "Odd"

number = int(input("Enter a number: "))
result = check_even_odd(number)
print(f"The number {number} is {result}.")

Check even or odd-Using Lambda Function

Method 6: Using List Comprehension

This method uses list comprehension to determine if a number is even or odd.

def check_even_odd(number):
    return ["Even", "Odd"][number % 2]

number = int(input("Enter a number: "))
result = check_even_odd(number)
print(f"The number {number} is {result}.")

Method 7: Using NumPy (External Library)

If you are using the NumPy library, you can leverage its functionality to check if a number is even or odd.

import numpy as np

def check_even_odd(number):
    return "Even" if np.mod(number, 2) == 0 else "Odd"

number = int(input("Enter a number: "))
result = check_even_odd(number)
print(f"The number {number} is {result}.")

List Of All Python Projects or Programs or Examples for Basic to Advanced users:

Click on the following link to view the complete list of all Python Projects or Programs or Examples.

Python Projects

List Of All Python Built-in Functions:

Click on the following link to view the complete list of built-in functions.

Python Built-in Functions