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.

1
  • Beside the point, but else: pass does nothing, so remove it. Commented Sep 9, 2024 at 19:09

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

0

Just use isinstance() function:

if isinstance(var, int):
    ...
else:
    ...

Comments

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

1 Comment

1) type is not a method, is a function. 2) type(x) is not reccomended since it doesn't support subclass checks
-1

you can user type() here.

for example :

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

1 Comment

Your code appears to be an exact copy of the code in the question, without formatting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.