- How to Check if a Number is Odd or Even using Python?
- Algorithm (Steps)
- Example
- Output
- Using Recursion
- Algorithm (Steps)
- Example
- Output
- Using Binary AND (&) operator
- Algorithm (Steps)
- Example
- Output
- Conclusion
- Python Function To Check Number is Even or Odd
- Even or Odd Number in Python
- Python Program to Find odd or even using function
- Python Function to check whether a given number is even or odd
- Conclusion
- How To Check If A Number In Python Is Even Or Odd
- isodd & iseven Functions
- Summary
- Primary Sidebar
- Footer
- Spreadsheets
- Python Code
- Apps
How to Check if a Number is Odd or Even using Python?
Python’s modulo (%) operator (also called the remainder operator) is useful to determine if a number is odd or even. We obtain the remainder of the division of a number by 2. If it is 0, it is even otherwise it is odd
Even Number − A number that can be divided by 2, leaving no remainders(remainder=0).
Odd Number − An odd number is one that cannot be divided by 2, so the remainder is 1.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
- Create a variable to store the input number.
- Use the if conditional statement to check whether the input number modulus 2 is equal to 0 using the modulus (%)operator(returns the remainder).
- Print even if the condition is true i.e, the remainder is 0.
- Else Print odd if the condition is false i.e, the remainder is not 0.
Example
The following program returns the whether the input number is an even or an odd number using the modulo (%) operator −
# input number inputNumber = 10 # checking whether the number modulus 2 is equal to 0 if inputNumber%2==0: # printing even if the remainder is 0 print(inputNumber, "is an even number") else: # else printing odd print(inputNumber, "is an odd number")
Output
On executing, the above program will generate the following output −
Using Recursion
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
- Create a function checkEvenOdd to check whether the number passed to it as an argument is an even or odd number.
- Use the if conditional statement to check whether the number is 0 and if it is 0 then the given number is even so return True.
- Use another if conditional statement to check whether the number is 1 and if it is 1 then the given number is odd so return False.
- Call the function again recursively by subtracting 2 from the given number.
- Pass the input number as an argument to the checkEvenOdd() and call the function. Use the if conditional statement to check whether the function returns True or False.
- Print even if the function returns True.
- Else print odd if the function returns False
Example
The following program returns the whether the input number is an even or an odd number using the recursive function −
# creating a function that accepts a number as an argument and # checks whether it is an odd or even number def checkEvenOdd(num): # checking whether the number is 0 if(num==0): # returning True, if the number is even return True # checking whether the number is 1 elif(num==1): # returning False, if the number is odd return False else: # Calling the function again recursively by subtracting 2 from the given number return checkEvenOdd(num-2) # input number inputNumber= 7 # passing inuput number as an argument to the checkEvenOdd() and calling it if(checkEvenOdd(inputNumber)): # printing even if the function returns true print(inputNumber, "is an even number") else: # else printing odd if the function returns false print(inputNumber, "is an odd number")
Output
On executing, the above program will generate the following output −
Using Binary AND (&) operator
The idea is to check if the last bit of the number is set. If the last bit is set, the number is odd, otherwise it is even.
As you can see, bitwise ANDing(&) a number through a 1 gives a 1 if the number is odd because the last bit is already set. Otherwise, 0 is returned as output.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
- Use the if conditional statement to check whether the binary and(&) operation between the number passed and 1 is equal to 0 using the & operator.
- If the condition is true, then the number is even and returns True.
- Else the given number is odd so return False.
- Create a variable to store the input number.
- Pass the input number as an argument to the checkEvenOdd() and call the function. Use the if conditional statement to check whether the function returns True or False.
- Print even if the function returns True.
- Else print odd if the function returns False.
Example
The following program returns the whether the input number is an even or an odd number using the binary AND (&) operator −
# creating a function that accepts a number as an argument and # checks whether it is an odd or even number def checkEvenOdd(num): # checking whether num&1 == 0 if num & 1 == 0: # Then the number is even so return True return True # Else the number is odd # Then the number is odd so return False return False # input number inputNumber= 12 # passing input number as an argument to the checkEvenOdd() and calling it if(checkEvenOdd(inputNumber)): # printing even if the function returns true print(inputNumber, "is an even number") # else printing odd if the function returns false print(inputNumber, "is an odd number")
Output
On executing, the above program will generate the following output −
12 is an even number 12 is an odd number
Conclusion
In this article, we learned how to use three different methods to determine whether a given number is even or odd. We learned how to check the last bit of a given number using bitwise operators. We learned how to recursively call the function.
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.
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.
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.
How To Check If A Number In Python Is Even Or Odd
How can you tell if a number is odd or even in Python?
The easiest way to determine if a number is even or odd in Python is to use the modulus operator . This operator, denoted as the percentage sign % displays the remainder from a division operation. To determine if a number is even simply apply my_number % 2 == 0 where my_number is your number and if this result is True then your number is even, otherwise, it is odd.
Here’s a simple example demonstrating how this works:
>>> im_even = 6 >>> im_even % 2 == 0 True >>> im_odd = 5 >>> im_odd % 2 == 0 False
isodd & iseven Functions
From this understanding you can even create your own isodd or iseven function depending on how often you need to perform this comparison in your code.
An example of your own isodd or iseven function could look something like this:
def iseven(n): return n % 2 == 0 def isodd(n): return n % 2 == 1
As you can see from the two functions above there is a slight difference in each where if the result of the modulo operation produces a zero for the iseven function then you know the number is even, whereas for the isodd function compares the modulo result to 1.
Both results from the functions would return True if the number inserted into the parameter satisfies the conditions.
Here is an example of the output when applying these functions:
>>> iseven(6) True >>> isodd(5) True >>> iseven(7) False >>> isodd(8) False
As you can see the operation is a little tidier and should make clear sense to anybody reading your code.
Summary
The modulus operator is another arithmetic operator available in Python and can help in determining whether a number is odd or even. Using the modulus operator with the number 2 will help to show if there is a remainder or not with any number.
The result from the operation my_number % 2 will either produce a 0 or 1, with 0 implying the number is even – as all even numbers can be divided by 2, and 1 implying the number is odd.
You can also wrap this modulo operation into your own custom functions isodd and iseven by extending it with a comparison to 0 or 1. This might help make your code more easy to read.
Primary Sidebar
Hello! My name is Ryan Sheehy the author of ScriptEverything.com
This website acts as a second brain of sorts for all the hacks and explorations I find when trying to solve problems at work.
Footer
Spreadsheets
I have been using spreadsheets since as early as 1996 and I continue to use this great productivity tool on a daily basis.
Python Code
I have been using Python since the late 1990’s due to the limitations of Excel’s VBA. I enjoy being able to wrangle and fetch data externally using Python.
Apps
Sometimes I play, hack and tinker with other applications to scratch an itch.
Copyright © 2023 ScriptEverything.com