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
float.is_integer()
Parameter
floating number
Returns
Boolean values (True or False)
Example Covering is_integer function
>>> 1.3.is_integer()
False
>>> 4.0.is_integer()
True
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
isinstance(object,type)
Parameter
object and data type
Return
Boolean value
Example
>>> isinstance(4.0,int)
False
>>> isinstance(4.0,float)
True
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.
Code
def is_integer(num):
if isinstance(num,int):
return True
if isinstance(num,float):
return num.is_integer()
return False
print(is_integer(100))
print(is_integer(17.0))
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.
Output
True True
Applying is_integer to NAN
>>> float("nan").is_integer()
False
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>
n.is_integer()
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
Code
def prime(n):
z=[]
for i in range (1, n+1):
if (n/i).is_integer():
z.append(i)
if len(z)==2:
print("Prime")
else:
print("Not Prime")
prime(23)
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.
Output
Prime
Why is_integer is not working in Python 3?
n = 9
a=((n/3)).is_integer()
print(a)
Traceback (most recent call last):
File "122590881/source.py", line 2, in <module>
a=((n/3)).is_integer()
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.
Frequently Asked Questions Related to is_integer Function in Python
Yes, is_integer is a built-in function.
>>>a=”str”
>>>a.is_integer()
The above code shows an error an attribute error. Because we can’t check strings using the is_integer function.
This code returns a result as False why?
The given number is a float. is_integer function only returns True for the integer value.
isinstance function is used to check whether the mentioned data type and value are the same.
Conclusion
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.