Register Login

Check if Variable is a Number in Python

Updated Sep 14, 2023

Python is a flexible, versatile, easy-to-handle programming language with many third-party modules. Python is a dynamically typed programming language. Users do not need to declare the variable type while initializing it. The compiler figures out the data type at runtime.

So, often users need to check whether their variables contain an integer. This article will provide all the methods to check whether a variable has an integer.

What is a Variable in Python?

A variable refers to a memory location, which users can use to store values. Users only need to assign a value to a variable and do not pass any data type. It acts as a container that requires command for its declaration. Whatever operation users perform on the variables affects the memory locations.

a = 5
a = "Python"

Here, we used the same variable name, "a," with two different values. We have reassigned the value of the second one to a new data type.

Method to Check Variables Containing Integer

Here is the list of methods that will help check whether a variable is of integer data type:

  1. Using type() function
  2. Using numbers.Number
  3. Using Isinstance() method
  4. Using Try-except block
  5. Using round() method

Method 1: Using the type() function

The function returns the type of the Python object. It is a built-in function in Python that accepts values passed as arguments for checking the data type.

Code Snippet:

a=56
print(type(a))
b=483.96
print(type(b))
c='This is Python'
print(type(c))

Output:

Explanation:

We applied the type() function to three distinct variables with different values. It returned integer, floating point, and string data types, respectively.

Checking with if-else:

a=input("Enter a positive integer=")
if (type(a)==int or float):
	print("You have entered a number")
else:
	print("You have not entered a number")

Output:

Explanation:

Though it will return the if block if one enters a number, this creates an issue because Python matches the truth values of the statements. Thus, as we have written float in our if block, it is similar to writing or True. It will always evaluate to True.

If we write the code like this:

a=input("Enter a positive integer=")
if type(a)==int or type(a)==float:
	print("You have entered a number")
else:
	print("You have not entered a number")

Output:

It will execute the else block even though we have entered a positive number.

Method 2: Using numbers.Number

It is an efficient way to check whether a variable contains an integer by using the Python "numbers" module. Users can use the isinstance() function with this Python module to get the solution.

Code Snippet:

import numbers
a=36
print(isinstance(a, numbers.Number))

Output:

Explanation:

We have tried to figure out whether variables are of integer data type using the Python "numbers" module. The isinstance() function just checks the type of the object with the specified type and returns true matches. It returned true since the variable contains a positive number.

Method 3: Using only the isinstance() function

The isinstance() function is a built-in function in Python. It returns true when the object data type matches the specified type. Else, returns false.

Code Snippet:

a=648b=76.989c="Hello, Python"
print(isinstance(a,int))
print(isinstance(b,int))
print(isinstance(c,int))

Output:

Method 4: Using try-except block

Another alternative to check variables is using the try-except block. Users set a given variable to an int or float in the try block. If it successfully executes the try block, it indicates that a variable is a number, i.e., an integer or floating point.

Syntax:

try=
	print() 
except=
	print()

Code Snippet:

a=753
try:
    res=int(a)
    print('The entered variable is an integer data type')
except:
    print('The entered variable is not an integer data type')

Output:

Explanation:

The try-except block works for both integers and floating points because we can cast an integer to a floating point and a floating point to an integer.

Method 5: Using round() function

The round() function in Python returns the nearest integer when users pass no values as the optional digit parameter.

Code Snippet:

def demo(num):
   if num==round(num):
     print("True")
   else:
     print("False")
a=830
b=45.7
demo(a)
demo(b)

Output:

Beyond every worry, you can get into the right solution to check your Python variables containing integers using any of the above methods. All these methods are easy, but this article will specifically highlight three methods, namely the isinstance() method, Numbers module, and try-except block as the most efficient methods.


×