5 Best Ways to Check if a Number is a Whole Number in Python

πŸ’‘ Problem Formulation: When working with numbers in Python, you might encounter situations where you need to determine if a number is a whole number or not. A whole number is an integer that is non-negative, including zero, without any decimal or fractional part. For example, given the input 4.0 or 3, your program should recognize both as whole numbers, whereas 3.14 should not be considered a whole number.

Method 1: Using the float.is_integer() Method

Python’s float object has a built-in method called is_integer() that returns True if the number is a whole number or False otherwise. This method is straightforward and requires that the number be of type float to work correctly.

Here’s an example:

num = 7.0
result = num.is_integer()
print(result)

Output: True

This snippet effectively uses the is_integer() method to check if the number stored in the variable num is a whole number. We convert integers to floats before applying the method to ensure accuracy.

Method 2: Modulus Operator Check

The modulus operator (%) in Python gives the remainder of the division between two numbers. By checking if a number modulo 1 gives a 0, you can determine if it’s a whole number without any decimal part.

Here’s an example:

num = 7.5
is_whole = (num % 1) == 0
print(is_whole)

Output: False

In the presented code, we use the % operator to get the remainder of num divided by 1. If the result is 0, is_whole is True, indicating that num is a whole number.

Method 3: Using Type Conversion to Integer

You can also check if a number is a whole number by converting it to an integer using the int() function and then comparing it to the original number. If they are equal, the number is a whole number.

Here’s an example:

num = 100.0
is_whole = int(num) == num
print(is_whole)

Output: True

This method checks if the integer conversion of num is still equal to the original number. Equality suggests that there was no fractional part lost in the conversion, meaning the number is whole.

Method 4: Using the math.floor() Function

The math module in Python includes the math.floor() function, which returns the largest integer less than or equal to a given number. If this integer is equal to the original number, then the number is a whole number.

Here’s an example:

import math

num = 42
is_whole = math.floor(num) == num
print(is_whole)

Output: True

By taking the floor of num using math.floor(), we drop any fractional part. If the floor value is the same as num, it confirms that num was already a whole number.

Bonus One-Liner Method 5: The int() Comparison in a Lambda Function

If you prefer a concise one-liner approach, you can use a lambda function that encapsulates the process of converting the number to an integer and performing a comparison, all in one breath.

Here’s an example:

is_whole_number = lambda x: int(x) == x
print(is_whole_number(8.0))
print(is_whole_number(5.4))

Output: True False

This lambda function, is_whole_number, compares the input x with its integer conversion, effectively determining if x is a whole number.

Summary/Discussion

  • Method 1: float.is_integer(). It’s simple and directly tied to float objects. However, it requires explicit conversion of numbers to float type if they aren’t floats already.
  • Method 2: Modulus Operator Check. This method is straightforward and works with any numeric type. It might not be as immediately clear to someone new to programming.
  • Method 3: Type Conversion to Integer. It’s clear and works without importing additional modules. It might not be efficient for large arrays of numbers.
  • Method 4: math.floor() Function. This requires importing the math module but provides mathematical clarity. It may involve a performance overhead for import when not already using the math module.
  • Method 5: Lambda Function. It’s compact and suitable for inline use. The trade-off is readability for those unfamiliar with lambda functions.