AI Workshop: learn to build apps with AI →
Strings and Numbers: How to check if a variable is a number in Python

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


You can check if a variable is an integer using the type() function, passing the variable as an argument, and then comparing the result to the int class:

age = 1
type(age) == int #True

Or using isinstance(), passing 2 arguments: the variable, and the int class:

age = 1
isinstance(age, int) #True

You can check if the number is a floating point number by comparing the result to float instead of int:

fraction = 0.1
type(fraction) == float #True

Lessons in this unit:

0: Introduction
1: Python Strings
2: Python Numbers
3: Python Booleans
4: How to check if a variable is a string in Python
5: ▶︎ How to check if a variable is a number in Python
6: Python, how to check if a number is odd or even
7: Python, Accepting Input
8: Python Constants