is number whole in python

To check if a number is a whole number or an integer, we can use the is_integer() method in Python. This method returns True if a number is an integer and False if it is a float or a non-numeric value.

Here's an example:

main.py
num = 10.0  # float
if num.is_integer():
    print("Number is a whole number")
else:
    print("Number is not a whole number")
127 chars
6 lines

Output:

main.py
Number is a whole number
25 chars
2 lines

In this example, we have assigned a float value 10.0 to the variable num. Then we have used the is_integer() method to check if the number is an integer or not. Since the number 10.0 is a whole number, the output is "Number is a whole number".

gistlibby LogSnag