Last Updated on May 16, 2024 by Roshan Parihar
To find the even number or odd number in Python, use the %
(modulo) operator with the Python if conditional statement.
The even number is the multiple of 2 and the odd is not a multiple of 2. Let’s find them with the examples given below.
Find The Even Number in Python
An even number is a number that is perfectly divisible by 2 without any remainder. If you want to find whether the number is even or not, you have to use the %
(modulo) operator as given below.
Examples 1
1 2 3 4 5 |
myNum = 8 if myNum % 2 == 0: print("The number is even number.") else: print("The number is odd number.") |
Output
The above example finds the number which is even using the Python %(modulo) operator. You can change the above code as per your requirement to find the number if even.
Check the Odd Numbers in Python
An odd number is not a perfect divisible of 2 and gives some remainder number also. If you want to check the odd numbers in Python, you have to use the %
(modulo) operator as given below.
Examples 2
1 2 3 4 5 |
myNum = 21 if myNum % 2 == 0: print("The number is even number.") else: print("The number is odd.") |
Output
The above output shows that the given number is odd. You can also check your number if it is odd or not using the above example.
Other Methods To Check Whether The Number Is Odd Or Even in Python
If you want to use other methods to find whether the number is even or odd. You can use &
(bitwise) operator of Python to check. You have to use the below-given method to get the type in the output.
Using Bitwise Operator in Python
To find your number, you have to use the bitwise operator with the if statement.
Examples 3
1 2 3 4 5 |
myNum = 21 if myNum & 1 == 1: print("The number is odd.") else: print("The number is even number.") |
Output
The above output shows that the given number is odd using a bitwise operator. You can also check the even number whether it is even or not using the same method. Just put your number in the above example and find its type in the output.
Can you talk a little about how the bitwise operator and the & determines even or odd?