Demystifying is_integer Function in Python

Here we are going to learn about one of the popular and useful built-in functions in Python. This function returns boolean values. Boolean values are True and then False. The function we are going to learn about now is is_integer. It checks whether the given value is integer or not and returns the integer value.

is_integer is a popular built-in function in Python. Built-in functions mean the function that is already available in the Python library. We don’t need to install that function separately. Is_integer function is useful to check the given value is an integer or not. If it is an integer value, it returns a boolean value True. If not, it returns a value False. Now we will briefly learn about this function.

What are the syntax, parameters, and return type of the is_integer function in Python?

Syntax of is_integer function in Python



floating number


Boolean values (True or False)

Example Covering is_integer function

>>> 1.3.is_integer()
>>> 4.0.is_integer()

In the above examples, 1.3 returns False because it is a floating number. 4.0 returns True because it is an integer. It only returns a boolean value as a result.

The above-mentioned examples are given in floating numbers only if we want to check the whole number. It shows a syntax error to us.

>>> 50.is_integer
SyntaxError: invalid syntax

See, we were given a whole number to check, but it shows an error. If we want to check the whole number using the is_integer function, we must use the isinstance function.

What is isinstance function?

isinstance function is a function that returns True if the given object is of mentioned data type. For example if i give a=isinstance(6,int) means it returns True. Because 6 is an integer.

Syntax for isinstance function



object and data type


Boolean value


>>> isinstance(4.0,int)
>>> isinstance(4.0,float)

In the above examples, 4.0, int returns False because the given number is a float data type. In the other example, we changed the datatype as float now the result is True.


def is_integer(num):
    if isinstance(num,int):
        return True
    if isinstance(num,float):
        return num.is_integer()
    return False

First declaring the function named is_integer. Next using the isinstance function to check whether the mentioned number is on a given int data type. If the number is of int data type, the result will be True. For that first, if statement is used. Next, if the statement is for the given number of float data types, it will return False. 100 and 100.0 both are integers only, so for both, it returns True only.



Applying is_integer to NAN

>>> float("nan").is_integer()

Nan is mean by Not a Number. It is applied to is_integer, but it returns False because nan is of float data type.

What happen if string is applied to is_integer?

If a string is applied to is_integer, it shows an attribute error. Because “str” has no attribute “is_integer”. Let us check it with a python code.

>>> n="python"
>>> n.is_integer()
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
AttributeError: 'str' object has no attribute 'is_integer'

We can see that it showed an error to us. So we can clearly understand that we can’t apply string to the is_integer function.

Using is_integer function in Python to Check the Prime number


def prime(n):
    for i in range (1, n+1):
        if (n/i).is_integer():
    if len(z)==2:
        print("Not Prime")

First, declaring a function named prime. Next, declaring an empty list z. for loop is used to iterate. First, it takes the given number and evaluates if the given condition becomes it will display the result as Prime. Here is_integer function is used to check the prime number.



Why is_integer is not working in Python 3?

n = 9
Traceback (most recent call last):
  File "122590881/", line 2, in <module>
AttributeError: 'int' object has no attribute 'is_integer

In python 3, is_integer shows an attribute error. It shows an error like “int” has no attribute is_integer. Because 9/3 returns a result 3, so is_integer is not working on it. If we give a code to display the output as 3.0, it will return the result.

1. Is is_integer is a built-in function?

Yes, is_integer is a built-in function.

2. What will be the output for the below program?

The above code shows an error an attribute error. Because we can’t check strings using the is_integer function.

3. >>>8.4.is_integer()
This code returns a result as False why?

The given number is a float. is_integer function only returns True for the integer value.

4. What is the use of the isinstance function?

isinstance function is used to check whether the mentioned data type and value are the same.


Here we have learned about the is_integer function in Python. It is one of the popular functions in python that is used to check whether the given is an integer or not. This function only returns boolean values. Boolean value means either True or False. The above-shown programs are just an example of the is_integer function.

Notify of
Inline Feedbacks
View all comments