AppDividend
Latest Code Tutorials

How to Print Type of Variable in Python

0

To determine the data type of a variable in Python, use the type() function. The type() method returns the class type of the argument(object) passed as a parameter. Use the print() function to print the variables.

Print Type of Variable in Python

To print the type of Variable in Python, use the type() function. The type() is a built-in function that returns the data type of the variable. For example, if the input is a list, it will return output as <class ‘list‘>, for the string, it will be <class ‘string‘>, etc.

For type(), you can pass a single argument, and the return value will be the class type of the argument.

Syntax

type(object)

Parameters

If the single object is passed to type(), the function returns its type.

Example

Let’s print the different types of variables.

str = "Welcome to Guru99"
run = 100
rr = 7.7
complex_num = 19j+21
player_list = ["VK", "RS", "JB", "RP"]
bat_name = ("A", "B", "C", "D")
duckworth = {"A": "a", "B": "b", "C": "c", "D": "d"}
lbw = {'Y', 'N', 'T', 'F'}

print("The type is : ", type(str))
print("The type is : ", type(run))
print("The type is : ", type(rr))
print("The type is : ", type(complex_num))
print("The type is : ", type(player_list))
print("The type is : ", type(bat_name))
print("The type is : ", type(duckworth))
print("The type is : ", type(lbw))

Output

The type is :  <class 'str'>
The type is :  <class 'int'>
The type is :  <class 'float'>
The type is :  <class 'complex'>
The type is :  <class 'list'>
The type is :  <class 'tuple'>
The type is :  <class 'dict'>
The type is :  <class 'set'>

You can see that we printed the different data types of different variables.

Python isinstance() Method

The isinstance() is a built-in Python method that returns True if a specified object is of the specified type. Otherwise, it returns False.

Syntax

isinstance(object, classtype)

Arguments

object: It is an object whose instance you are comparing with class type. It will return True if the type matches otherwise false.

class type: It is a type or a class or a tuple of types and/or classes.

Example

In this example, we will compare the float value with type float, i.e., 19.21 value will be compared with type float.

fvalue = 19.21
dt = isinstance(fvalue, float)
print("It is a float value:", dt)

Output

It is a float value: True

And it returns True.

Using the isinstance() method, you can test for string, float, int, list, tuple, dict, set, class, etc.

Don’t use __class__

In Python, names that start with underscores are semantically not a part of the public API. It’s the best practice for users to avoid using them except when necessary.

Don’t do the following.

class Excol(object):
    def dol(self):
        self.__class__

Instead, do the following.

class Excol(object):
    def dol(self):
        type(self)

That means always use the type() function to determine the data type of variable in Python.

That is it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.