How To Check If A Number Is Odd In Python

In this tutorial, we will learn how to check if a number is odd in Python. You will learn a few different methods to achieve this task in a simple and efficient way. By the end of this tutorial, you will be proficient in checking whether a given number is an odd number or not.

Method 1: Using the Modulus Operator

The modulus operator (%) returns the remainder of the division of one number by another. When a number is divided by 2 and the remainder is 1, it means the number is odd. In this method, we will use this property to determine if a given number is an odd number.

Method 2: Using Bitwise AND Operator

The bitwise AND operator (&) is another method to check if a given number is odd or even. When we perform a bitwise AND operation between an odd number and 1, the result is always 1, but in the case of an even number, the result is always 0. We will use this property to check if the given number is odd.

Method 3: Using the NumPy library

NumPy is a powerful library for numerical computing in Python. We can use numpy.remainder a function to find if a given number is odd or even. If the remainder of the number divided by 2 is 1, the number is odd.

First, you need to install NumPy if you haven’t already:
pip install numpy
Now, we will use NumPy to find out if the given number is odd:

Full Code:

Output:

True
True
True

Conclusion

In this tutorial, we learned three different methods to check if a number is odd in Python. You can choose any method depending on your requirements. The first two methods are simple and fast, while the third method uses the NumPy library, which is useful for working with arrays and large datasets.