Created: November-09, 2021 This tutorial will discuss the methods to check whether a number is even or odd in Python. By definition, a whole number that is completely divisible by 2 is known as an even number. In other words, a whole number is even if after division by 2 we get 0 as remainder. In mathematics, all whole numbers other than even
numbers are odd numbers. According to another definition, even numbers are called even because we can evenly split them into two halves. For example, 10 is an even number because we can evenly divide 10 into two halves of 5. On the contrary, 11 cannot be divided into two whole equal numbers because 11 is an odd number. In python, the modulus operator % divides the first number by the second number and gives us the remainder of the division. There is a way to determine whether a
number is odd or even by checking if the remainder after division is equal to 0 or not. The following code snippet shows us how to check whether a number is even or odd with the modulus operator %. Output: We defined the check(num) that checks whether the num is completely divisible by 2 with the help of the % operator. If the remainder is equal to 0, the number is even. If the remainder isn’t 0, the number is
odd. Another clever way of determining whether a number is even or odd is by using the bitwise AND operator &. As we all know, everything in
the computer is stored in the form of 1s and 0s, or binary language in other words. The bitwise AND operator & converts the values to binary and then performs an AND operation on each bit of the binary expression.Check Whether a Number Is Even or Odd With the % Operator in Python
Check Whether a Number Is Even or Odd With the & Operator in Python
For example, the binary value of the decimal number 11 is (1011), and the decimal number 1 is (0001), respectively. If we perform bitwise and operation on these two decimal numbers, the & operator takes each bit in both numbers, performs AND operator on them, and returns the results bit-by-bit. In the above case, the returned value would be (0001), which is equal to 1 in decimal.
Another interesting fact about this phenomenon is that if we take the bitwise AND operation of an even number and 1, the result would always be 0. Otherwise, if we take the bitwise AND operation of an odd number and 1, the result would always be 1.
The sample code below shows how we can use the bitwise AND operator & to check whether a number is odd or even.
def check(num): if num & 1 == 0: print("even") else: print("odd") check(33)Output:
oddWe defined the check(num) that checks whether the bitwise AND operation of num and 1 is equal to 0 or not. If the result is equal to 0, the number is even. If the result isn’t 0, the number is odd.
Related Article - Python Math
Python Basic: Exercise-21 with Solution
Write a Python program to find whether a given number (accept from the user) is even or odd, print out an appropriate message to the user.
Pictorial Presentation of Even Numbers:
Pictorial Presentation of Odd Numbers:
Sample Solution:-
Python Code:
num = int(input("Enter a number: ")) mod = num % 2 if mod > 0: print("This is an odd number.") else: print("This is an even number.")Sample Output:
Enter a number: 5 This is an odd number.Even Numbers between 1 to 100:
Odd Numbers between 1 to 100:
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to get a string which is n (non-negative integer) copies of a given string.
Next:
Write a Python program to count the number 4 in a given list.
Python: Tips of the Day
Unknown Arguments Using *arguments:
If your function can take in any number of arguments then add a * in front of the parameter name:
def myfunc(*arguments): for a in arguments: print a myfunc(a) myfunc(a,b) myfunc(a,b,c)