Python Function To Check Number is Even or Odd

In this Python Tutorial, we will learn the Python function to check whether a given number is even or odd. In addition, we will learn about even or odd numbers in Python.

Even or Odd Number in Python

In mathematics, integers can be classified into two major categories, namely: even and odd numbers. An even number is an integer that is exactly divisible by 2, i.e., when it’s divided by 2, there is no remainder. On the other hand, an odd number is an integer that is not evenly divisible by 2.

Python Program to Find odd or even using function

  • To determine whether a number is even or odd, we use the modulus operator (%), which returns the remainder of the division. In Python, the modulus operator is the percentage sign (%).
  • When a number is divided by 2, if it leaves a remainder of 0, then it is an even number.
  • If it leaves a remainder of 1, then it is an odd number.

Python Function to check whether a given number is even or odd

Let’s now implement the function in Python:

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

print(check_even_odd(7))  
print(check_even_odd(8))  
print(check_even_odd(0))  
  • In this code snippet, we defined a function named check_even_odd which takes a single argument number.
  • Inside the function, we used an if-else statement with the condition number % 2 == 0. This condition will be true if number is evenly divisible by 2 (i.e., it’s an even number), and the function will return the string “Even”.
  • If the condition is false (i.e., number is not evenly divisible by 2, making it an odd number), the function will return the string “Odd”.
  • Next, we can see that the function correctly identifies 10 as an even number, 13 as an odd number, and 0 (which is considered even) as an even number.
READ:  indexerror: string index out of range in Python

Output:

Python Function To Check Number is Even or Odd

Conclusion

In this article, we’ve learned how to create a Python function that can determine whether a given number is even or odd.

You may also like to read the following Python tutorials.