Python – check if value/variable is integer

 

By using is isinstance(var, type) function we can check a given value/variable is integer or not. isinstance will either written True or False.

Here is and an example:

>>> isinstance(10, int)
True
>>> var_a=10
>>> isinstance(var_a, int)
True
>>> isinstance('a', int)
False

To check if variable is a number use following example:

>>> isinstance(1.0, (int, long, float))
True

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s