Determin the variable type

linkaiyi
Follow

Determin the variable 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
Object has 0 attachments

Was this article helpful?

This article is viewed 28 times!

Recent Viewed Articles

0 Comments

Leave a Comment

Support