Open In App

Check if a Number is a Whole Number in Python

Last Updated : 01 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Checking if a Number is a Whole Number in Python means determining whether the number has no fractional part, using custom logic instead of relying on assumptions about its type.

For example, a number like 7.0 is considered whole, while 7.5 is not. Let’s explore various methods to do it accurately.

Using modulus operator(%)

This method checks whether there’s any remainder when a number is divided by 1. If there’s no remainder, the number is considered a whole number. It is one of the most efficient approaches for identifying whole numbers in both integer and float formats.

Python
a = 7.0
if a % 1 == 0:
    print("Whole number")
else:
    print("Not a whole number")

Output
Whole number

Explanation: If a number has a decimal part, dividing it by 1 will leave a remainder. So, if a % 1 == 0, the number has no decimal part and is a whole number otherwise, it’s not.

Comparing with int()

This approach compares the number with its integer version using the int() function. If both values are equal, the number has no fractional part and is treated as a whole number.

Python
a = 2.5
if a == int(a):
    print("Whole number")
else:
    print("Not a whole number")

Output
Not a whole number

Explanation: int() function removes the decimal part, so if a == int(a) is true, the number has no fractional part and is a whole number otherwise, it's not.

Using .is_integer()

Python’s float type provides the .is_integer() method, which directly returns True if the float value has no decimal component. This method is specifically designed for float values and offers a clean, Pythonic way to determine if a number is whole.

Python
a = 7.9

if isinstance(a, float) and a.is_integer():
    print("Whole number")
elif isinstance(a, int):
    print("Whole number")
else:
    print("Not a whole number")

Output
Not a whole number

Explanation: .is_integer() method returns True if a float has no decimal part. The code checks if the value is a float and whole or an integer. If not, it's not a whole number.

Using math.floor() or math.ceil()

By comparing the floor and ceiling values of a number, we can check if it is a whole number. If both the floor and ceil are equal, it means the number has no decimal part.

Python
import math
a = 7.0

if math.floor(a) == math.ceil(a):
    print("Whole number")
else:
    print("Not a whole number")

Output
Whole number

Explanation: If math.floor(a) equals math.ceil(a), the number has no decimal part and is whole otherwise, it's not.

Related articles


Next Article

Similar Reads