How to determine a Python variables type?

How to determine a Python variables type?

Use the type() builtin function:

>>> i = 123
>>> type(i)
<type int>
>>> type(i) is int
True
>>> i = 123.456
>>> type(i)
<type float>
>>> type(i) is float
True

To check if a variable is of a given type, use isinstance:

>>> i = 123
>>> isinstance(i, int)
True
>>> isinstance(i, (float, str, set, dict))
False

Note that Python doesnt have the same types as C/C++, which appears to be your question.

You may be looking for the type() built-in function.

See the examples below, but theres no unsigned type in Python just like Java.

Positive integer:

>>> v = 10
>>> type(v)
<type int>

Large positive integer:

>>> v = 100000000000000
>>> type(v)
<type long>

Negative integer:

>>> v = -10
>>> type(v)
<type int>

Literal sequence of characters:

>>> v = hi
>>> type(v)
<type str>

Floating point integer:

>>> v = 3.14159
>>> type(v)
<type float>

How to determine a Python variables type?

It is so simple. You do it like this.

print(type(variable_name))

Leave a Reply

Your email address will not be published. Required fields are marked *