In this Python tutorial, we will discuss how to check if a variable is an integer in Python. We will also discuss, how to check if a number is an integer in Python.
Python check if a variable is an integer
To check if the variable is an integer in Python, we will use isinstance() which will return a boolean value whether a variable is of type integer or not.
Example:
my_variable = 56
print(isinstance(my_variable, int))
After writing the above code (python check if the variable is an integer), Ones you will print ” isinstance() “ then the output will appear as a “ True ”. Here, isinstance() will check if a variable is an integer or not and if it is an integer then it will return true otherwise false.
You can refer to the below screenshot to check if the variable is an integer.
This is how to check if a variable is an integer in Python.
Let us explore, different methods to check if a variable is an integer in Python.
How to check if a variable is an integer in Python
1. Using isinstance()
The isinstance() function in Python is a built-in function that checks if a variable is an instance of a specific class or a subclass thereof. Here’s how we can use isinstance() to check if a variable is an integer:
def is_integer(n):
return isinstance(n, int)
print(is_integer(5)) # Outputs: True
print(is_integer(5.5)) # Outputs: False
print(is_integer("5")) # Outputs: False
In this example, is_integer() returns True when the variable is an integer and False otherwise. You can see the output like below:
2. Using type()
While isinstance() checks if a variable is an instance of a specific class or a subclass thereof, the type() function strictly checks if a variable is an instance of a specific class and not its subclasses. Here’s how to use type() to check if a variable is an integer:
def is_integer(n):
return type(n) is int
print(is_integer(5)) # Outputs: True
print(is_integer(5.5)) # Outputs: False
print(is_integer("5")) # Outputs: False
Once you execute the code, you can see the output:
3. Using the math module
We can also use the Python math module to check if a number is an integer. This method works well when we want to check if a floating-point number is, in fact, an integer.
import math
def is_integer(n):
if isinstance(n, float):
return n.is_integer()
elif isinstance(n, int):
return True
else:
return False
print(is_integer(5)) # Outputs: True
print(is_integer(5.0)) # Outputs: True
print(is_integer(5.5)) # Outputs: False
print(is_integer("5")) # Outputs: False
4. Exception handling
Sometimes, we might want to check if a variable can be converted to an integer, even if it’s not already one. This can be useful when reading data from files or user input. We can use a try/except block to do this:
def is_integer(n):
try:
int(n)
return True
except ValueError:
return False
print(is_integer(5)) # Outputs: True
print(is_integer(5.5)) # Outputs: True
print(is_integer("5")) # Outputs: True
print(is_integer("5.5")) # Outputs: False
5. Checking if a float is a whole number
If you’re dealing with floats and want to check if they could be an integer (i.e., they are a whole number), you can use the is_integer() method available on all float instances:
def is_integer(n):
return isinstance(n, float) and n.is_integer()
print(is_integer(5.0)) # Outputs: True
print(is_integer(5.5))
How to check if a number is an integer in Python
In Python you can check if a number is an integer by using the isinstance()
built-in function. The isinstance()
function checks if the object (first argument) is an instance or subclass of classinfo class (second argument).
Here is a simple Python function that checks if a number is an integer:
def is_integer(n):
if isinstance(n, int):
return True
else:
return False
You can then use this function to check various numbers like so:
print(is_integer(10)) # prints: True
print(is_integer(10.0)) # prints: False
print(is_integer('10')) # prints: False
The function is_integer
checks whether the input n
is an integer.
- If the input
n
is an integer,isinstance(n, int)
isTrue
and the function returnsTrue
. - If the input
n
is not an integer,isinstance(n, int)
isFalse
and the function returnsFalse
.
The print
statements then print the output of the function for different inputs.
It’s important to note that is_integer()
will return False
for floating point numbers even if they represent a number that could be an integer (like 10.0
). If you would like it to return True
for those cases, you would need to write your function slightly differently:
def is_integer(n):
if isinstance(n, int):
return True
elif isinstance(n, float) and n.is_integer():
return True
else:
return False
And you can use them like below:
print(is_integer(10)) # prints: True
print(is_integer(10.0)) # prints: True
print(is_integer(10.5)) # prints: False
print(is_integer('10')) # prints: False
In this second version of the function, is_integer()
checks additionally if n
is a float and if it represents an integer number. If both conditions are true, it returns True
.
This is how to check if a variable is an integer in Python.
Conclusion
In this tutorial, we have learned, how to check if a number is an integer in Python and also how to check if a variable is an integer in Python.
You may also like:
- How to convert an integer to string in Python
- Convert Integers to Datetime in Pandas in Python
- convert floats to integer in Pandas
- Python program to print prime numbers
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.