0

so I need a program that checks if a variable is integer and it's not float data.

I tried this:

var = 2.5
if var is int :
    print(var)
else :
    pass

but this didn't work. can you help me on this? thanks.

Parsa Ad
  • 91
  • 6

4 Answers4

6

you can use simply the isinstance check

if isinstance(tocheck,int):
    print("is an int!")
elif isinstance(tocheck,float):
    print("is a float")
else:
    print("is not int and is not float!")

you could also use the type function but it doesn't check for subclasses so it's not recommended but provide anyways:

if type(x)==int:
    print("x is int")
elif type(x)==float:
    print("x is float")
else:
    print("x is neither float or int")

here are some useful links

XxJames07-
  • 1,833
  • 1
  • 4
  • 17
0

Just use isinstance() function:

if isinstance(var, int):
    ...
else:
    ...
aliwimo
  • 156
  • 2
  • 8
0

you can use the type method

https://www.programiz.com/python-programming/methods/built-in/type

print(type(var))

or

var = 2.5
if type(var) is int :
    print(var)
else :
    pass
Oz Cohen
  • 873
  • 1
  • 8
  • 11
  • 1
    1) type is not a method, is a function. 2) type(x) is not reccomended since it doesn't support subclass checks – XxJames07- Jul 03 '22 at 12:46
-1

you can user type() here.

for example :

var = 2.5 if type(var) is int : print(var) else : pass

electron
  • 1
  • 3