How To Check Type Of Variable In Python?

Python does not have a type() function to check the type of a variable. Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.

Checkout this video:


In computer programming, data type is an important concept. Variables can store data of different types, and different types can do different things.

Knowing the data type of a variable is important because it tells you what kind of values the variable can store and what kind of operations you can perform on it. In Python, there are two ways to check the data type of a variable:
-type() function
-isinstance() function

We will discuss both these functions in this article.

What is Python?

Python is a programming language with objects, modules, threads, exceptions and automatic memory management. It supports multiple programming paradigms, including structured, object-oriented, and functional programming.

What are the benefits of using Python?

Python is a high-level, interpreted, general-purpose programming language, created on December 3, 1989, by Guido van Rossum, with a design philosophy entitled, “There’s only one way to do it, and that’s why it works.”

In Python, that means explicit is better than implicit. It also gives rise to the infamous Python telegraph pole analogy attributed to creator Guido van Rossum, which goes like this:

There is beauty in π, elegance in an all-numeric telephone keypad . . . I am attracted to the simpleness of a perfect poker face, and the serenity of perfect punctuation mark placement. Just as art to be appreciated or not, comments to be enjoyed or ignored, so is Python beautiful or ugly. I find Python attractive because it rejects exuberance.”

What are the different data types in Python?

Python has multiple data types including integers, floats, strings, and booleans. In this article, we’ll take a look at how to check the type of a variable in Python.

Integers are whole numbers and can be positive or negative. Floats are fractional numbers and can also be positive or negative. Strings are sequences of characters and can be enclosed in single or double quotes. Booleans are values that can only be True or False.

To check the type of a variable in Python, you can use the type() function. This function returns the data type of the argument passed to it.

Here are some examples:

>>> type(10)

>>> type(3.14)

>>> type(‘Hello’)

>>> type(True) # Note that booleans are written with a capital ‘T’
# (True) and a capital ‘F’ (False)


What is typecasting?

Typecasting is a method of changing an entity from one data type to another. It is usually done when we need to store data that has been created in a format that is different from the format we need, or when we need to change an entity to a format that can be read by another program. For example, we may want to take an integer value and store it as a string so that we can print it out.

How to check the type of a variable in Python?

Python is a strongly typed language, meaning that every value has a specific type. You can use the type() function to check the type of a variable. The following example shows how to check the type of a variable:


Why is it important to know the type of a variable?

There are several reasons why it is important to know the type of a variable in Python. First, different data types have different properties. For example, integers can be added together, but strings cannot. If you try to add two strings together, you will get an error. Second, the type of a variable can affect how it is used. For example, some functions only work with certain data types. If you try to use a function on a variable of the wrong data type, you will get an error. Finally, some data types take up more memory than others. This can be important if you are working with large datasets.

What are the different type-checking functions in Python?

Python has several built-in type-checking functions that can be used to check the type of a variable. The most common are the isinstance() and issubclass() functions.

isinstance(obj, class_or_type_or_tuple)

Returns True if obj is an instance of any of the classes or types in class_or_type_or_tuple. If not, it returns False.

issubclass(subclass, class_or_type)

Returns True if subclass is a subclass of class or type. If not, it returns False.

How to use type-checking functions in Python?

Python’s built-in “isinstance” function can be used for type-checking. This function takes two arguments. The first is the value to check, and the second is the expected type (or a tuple of expected types, if more than one is desired). For example, let’s say we have a variable “x” that we want to check is a float:

x = 1.0

isinstance(x, float)

# Returns True
If we want to check that “x” is either a float or an int, we can do this:

isinstance(x, (float, int))
# Returns True


As you can see, there are many ways to find out the type of variable in Python. It all depends on what information you are looking for and how you want to use it. In general, the most useful methods are type(), isinstance(), and issubclass(). These three methods can give you a lot of information about the type of an object, and they are also easy to use.

Scroll to Top