• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
June 23, 2022 |2.9K Views

How to check if a number is odd or even in Python

Description
Discussion


In this video, we will learn how to find odd numbers using python. We divided this video into 5 sections to find prime numbers.

Below is the list of approaches that we will cover in this section:

1. What are odd numbers.
2. How to find odd-even numbers using modulus.
3. Dry run of code
4. Code in python using modulo
5. Code to get odd-even numbers using 'AND' operator.

Modulus Operator Approach:  

When we need to calculate the remainder of an integer after it has been divided by any divisor, we utilize this operator.
Even Number: A number that is totally divisible by two. As a result, there are no remainders.
Odd Number: A number that is not divisible by two, resulting in a single remainder.

To find the even or odd, we use the modulus operator. If num percent 2 == 0, the remainder is 0, indicating that the given number is even and returning True. Otherwise, if num percent 2!= 0, the remainder is not zero, indicating that the provided number is odd and returning False.

BINARY AND (&) operator Approach:

For this method, we just need to use the & operator, which is a bitwise operator, and it works the same way as the % operator for this particular case.

Python program to determine whether a given number is even or odd: https://www.geeksforgeeks.org/python-program-to-determine-whether-a-given-number-is-even-or-odd-recursively/