Check if a Number is a Whole Number in Python
Last Updated :
01 Jul, 2025
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")
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")
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")
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")
Explanation: If math.floor(a) equals math.ceil(a), the number has no decimal part and is whole otherwise, it's not.
Related articles
Similar Reads
Python Program to Check If a Number is a Harshad Number Harshad Numbers can be divided by the sum of its digits. They are also called Niven Numbers. For instance, 18 is a Harshad Number as it can be divided by 9, the sum of its digits (8+1=9). In this article, we will discuss various approaches to determine whether the given number is a Harshad Number in
2 min read
Python Check If String is Number In Python, there are different situations where we need to determine whether a given string is valid or not. Given a string, the task is to develop a Python program to check whether the string represents a valid number.Example: Using isdigit() MethodPython# Python code to check if string is numeric
6 min read
Python Program to Check Number is a Power of Two we will discuss how to write a Python program to determine whether a given number is a power of two. A number is said to be a power of two if it can be expressed in the form of 2n, where n is a non-negative integer.Examples: Pythondef is_power_of_two(n): if n <= 0: return False return (n & (n
4 min read
Python Program to Check if a Number is Odd or Even Even Numbers are exactly divisible by 2 and Odd Numbers are not exactly divisible by 2. We can use modulo operator (%) to check if the number is even or odd. For even numbers, the remainder when divided by 2 is 0, and for odd numbers, the remainder is 1.In this article, we will learn how to check if
2 min read
Python program to check if number is palindrome (one-liner) In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it's a Palindrome number otherwise it would be False. Let's discuss how to find whether a number is palindrome or not in this article. Input1: test_number
3 min read