Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

Look at the below code:

inNumber = somenumber

inNumberint = int(inNumber)

if inNumber == inNumberint:

    print "this number is an int"

else:

    print "this number is a float"

Something to that effect. 

Are there any more pleasant-looking approaches to do this?

1 Answer

0 votes
by (26.4k points)

You can use isinstance:

>>> x = 12

>>> isinstance(x, int)

True

>>> y = 12.0

>>> isinstance(y, float)

True

So:

>>> if isinstance(x, int):

        print 'x is a int!'

x is a int!

In case of long integers, try:

>>> x = 12L

>>> import numbers

>>> isinstance(x, numbers.Integral)

True

>>> isinstance(x, int)

False

Are you interested to learn the concepts of Python? Join the python training course fast!

Related questions

+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 8, 2021 in Python by ashely (50.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...