 # How To Check If A Number Is An Int Or Float in Python There are many ways to check if a number is an int or float in Python because this is basic knowledge for you to start with Python or another programming language. Let’s learn about some ways to solve this problem with the explanation and examples below.

## What is an int in Python?

An integer is a number that does not have decimal.

Look at the example below.

x = 10 # x is an integer
y = 10.5 # y is not an integer

print(type(x)) # <class 'int'>
print(type(y)) # <class 'float'>

## What is a float in Python?

A float is a number which has decimal.

Look at the example below.

x = 10.5 # x is a float
y = 10 # y is not a float

print(type(x)) # <class 'float'>
print(type(y)) # <class 'int'>

## Check if a number is an int or float in Python

There are some solutions that can help you check if a number is an int or float in Python.

### Use the int() method

This method helps you convert a number to an integer.

Syntax:

int(number)

Parameters:

• number: The number you want to convert

Return value: An integer.

You will use this method to check if the current number equals another number after converting to int or not.

def checkType(x):
if x == int(x):
print('This number is an int.')
else:
print('This number is a float.')

x = 10
y = 10.5

# Try this function with the number 'x'
checkType(x)

# Try this function with the number 'y'
checkType(y)

Output

This number is an int.
This number is a float.

### Use the isinstance() method

This method helps you check if the current variable is the specified type or not.

Syntax:

isinstance(object,type)

Parameters:

• object: An object
• type: A type or class.

Return value: True or False.

You will use this method to check if the current number is an int or float.

Look at the example below.

def checkType(x):
if isinstance(x, int): # Another way, you can check if x is float or not
print('This number is an int.')
else:
print('This number is a float.')

x = 10
y = 10.5

# Try this function with the number 'x'
checkType(x)

# Try this function with the number 'y'
checkType(y)

Output:

This number is an int.
This number is a float.

### Use the in operator

To use this method, you must use str() method to convert the number to string first.

Syntax:

str(number)

Parameters:

• number: The number you want to check.

Return value: A String.

After that, you check if the new number has a colon or not by the in operator. If it returns True, the number is a float, otherwise an int.

def checkType(x):
x = str(x)
if '.' in x:
print('This number is a float.')
else:
print('This number is an int.')

x = 10
y = 10.5

# Try this function with the number 'x'
checkType(x)

# Try this function with the number 'y'
checkType(y)

Output

This number is an int.
This number is a float.

### Use the type() method

The type() method returns the type of the object.

Syntax:

type(object)

Parameters:

• object: The object you want to check.

Return value: Type or class.

You will use this method to check the type of the specified number is an int or float.

Look at the example below.

def checkType(x):
if type(x) == float:
print('This number is a float.')
else:
print('This number is an int.')

x = 10
y = 10.5

# Try this function with the number 'x'
checkType(x)

# Try this function with the number 'y'
checkType(y)

Output

This number is an int.
This number is a float.

## Summary

To check if a number is an int or float in Python, you can use the int() method, the isinstance() method, the type() method, or the in operator. These solutions are very easy to do even for beginners. So, choose the solution that is best for you. We hope this guide is helpful to you. Thanks!

Maybe you are interested: