Modified: August 19, 2023# How to Check if a Variable is a Number in Python?

**1. Making Use of the type() Function**

**โ Defining the Function**

**How it Works**

**2. Making Use of the isinstance() Function**

**โ Defining the Function**

**How it Works**

**Which One to Use?**

**Frequently Asked Questions (FAQs)**

In Python, variables can hold various types of data, including integers (int) and floating-point numbers (float).

When dealing with dynamic data, itโs crucial to ascertain whether a variable contains a numeric value before performing mathematical operations.

We will discuss two methods that help you achieve this goal: the type() function and the isinstance() function.

```
def is_number(variable):
"""
This function checks if a variable is a number.
Args:
variable: The variable to check.
Returns:
True if the variable is a number, False otherwise.
"""
return type(variable) in (int, float)
```

The `type()`

function returns the type of the given variable. If the type is either int or float, the function returns True, indicating that the variable is a number.

Otherwise, it returns False.

**Example Code:**

```
def is_number(variable):
"""
This function checks if a variable is a number.
Args:
variable: The variable to check.
Returns:
True if the variable is a number, False otherwise.
"""
return type(variable) in (int, float)
# Examples
number = 10
result_number = is_number(number)
string = "Hello"
result_string = is_number(string)
print(f"Is {number} a number? {result_number}")
print(f"Is {string} a number? {result_string}")
```

```
Is 10 a number? True
Is Hello a number? False
```

```
def is_number(variable):
"""
This function checks if a variable is a number.
Args:
variable: The variable to check.
Returns:
True if the variable is a number, False otherwise.
"""
return isinstance(variable, (int, float))
```

The `isinstance()`

function verifies if the variable is an instance of either the int or float class.

If the variable is indeed an instance of either of these classes, the function returns True; otherwise, it returns False.

**Example Code:**

```
def is_number(variable):
"""
This function checks if a variable is a number.
Args:
variable: The variable to check.
Returns:
True if the variable is a number, False otherwise.
"""
return isinstance(variable, (int, float))
# Examples
number = 20
result_number = is_number(number)
string = "Good Bye"
result_string = is_number(string)
print(f"Is {number} a number? {result_number}")
print(f"Is {string} a number? {result_string}")
```

```
Is 20 a number? True
Is Good Bye a number? False
```

The choice between the `type()`

and `isinstance()`

methods depends on your coding style and specific requirements.

The `type()`

function offers a concise approach, while the `isinstance()`

function provides more explicit validation.

**Can these methods distinguish between integer and floating-point numbers?**Yes, both methods can distinguish between integers and floating-point numbers.**What happens if I use the type() function on a non-numeric variable?**The type() function will return the type of the variable, which might not be int or float.**Are there any performance differences between these two methods?**Generally, the type() function is slightly faster than the isinstance() function due to its simplicity.**Can I use these functions to check for complex numbers as well?**No, these methods are designed to check for int and float types only. To check for complex numbers, you would need a different approach.**Can I create a custom function to check for other numeric types, such as decimals?**Yes, you can create custom functions using similar logic to check for other numeric types like decimals or fractions.