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_oddwhich takes a single argument
- Inside the function, we used an if-else statement with the condition
number % 2 == 0. This condition will be true if
numberis 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.,
numberis 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.
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.
- Python if else with examples
- Create a tuple in Python
- Python Append List to Another List
- Find the sum of digits of a number in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.