0

I have just started in Python.

Is there a way to check whether a particular number is integer or not. e.g. x=12.33

Anshika Singh
  • 994
  • 12
  • 20
Kyle
  • 63
  • 7
  • Does this answer your question? [What's the canonical way to check for type in Python?](https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python) – Jannes Carpentier Aug 06 '20 at 14:13
  • A lot of the answers are answering the question of how to check whether a number is an integer. They are ignoring that the variable x in your example is a float.There are two number types in Python: Integer and Float. In your example, 12.33 is a float. So all of the answers so far will return `False` since 12.33 is not an integer. Integer is a whole number such as `12`. If you already knew that, great! Regardless: welcome to Python. – patmcb Aug 06 '20 at 14:20
  • 2
    Does this answer your question? [Check if a number is int or float](https://stackoverflow.com/questions/4541155/check-if-a-number-is-int-or-float) – Tomerikoo Aug 06 '20 at 14:27
  • The answer selected is actually the wrong answer to this question. and the question itself is a duplicate. – Akshay Sehgal Aug 06 '20 at 14:30
  • 2
    We can't know if it's the wrong answer as the question is missing alot of information and shouldn't have (7!!!) answers – Tomerikoo Aug 06 '20 at 14:31
  • 1
    @Tomerikoo, I am sorry.. as am just a beginner to stack overflow. I was not knowing like how to ask a question and all. That selected answer worked for me so just accepted it. – Kyle Aug 06 '20 at 14:35
  • Have a look in [ask] and maybe take the [tour]. I think the information your question is missing is what types are your inputs. Can you assure it's a float and want to check if it's a whole number? Can it be a string, int etc. This is relevant information – Tomerikoo Aug 06 '20 at 14:37
  • I will have a look. Thank you @Tomerikoo – Kyle Aug 06 '20 at 14:39

7 Answers7

1

There are multiple ways in python to solve this.

If you want to confirm the datatype then do then the recommended way is -

x = 12.33

isinstance(x, int)
#False

isinstance(x, float)
#True

The above approach is agnostic to the type of object x is to begin with.

You can also try the following ways -

x = 12.33

type(x) == int
#False

type(x) == float
#True

## This approach is not recommended as the is_integer method is available only for float type objects
x.is_integer()
#False

If the variable is originally a string, but you still wanna check if the element is potentially a numeric type then -

x = '12.33' 

x.isnumeric()
#False

x.isdigit()
#False

x.isdecimal()
#True
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
0

if type(x) == int. type(variable) returns type of object. You just compare it with what you want

vrnvorona
  • 397
  • 3
  • 8
0

If the variable is already a number (float), you can use float.is_integer()

>>> i = 4.0
>>> i.is_integer()
True

>>> i = 4.1
>>> i.is_integer()
False
Aziz
  • 20,065
  • 8
  • 63
  • 69
0

You can use type(var) to check the data type.

x = 12.33

if type(x) == int:
    print("Integer")
elif type(x) == float:
    print("Float")
Maurice
  • 13
  • 6
0

Use type(<variable>) function to determine the data type of it (Use if case for this). It will help!

alphaX64
  • 71
  • 1
  • 7
0

As some answers have suggested you can check the type of the variable, for instance:

x = 2
type(x) == int

however, if you want to check if the number is an integer even if it's stored as a float you could cast it to a float and compare it with itself. i.e.

x = 2.000
int(x) == x
# True

y = 1.2043
int(y) == y
# False

If it is not an integer value the typecasting will truncate the number to just the integer part and it will no longer be equal. It's worth noting this may not work for very large integers due to floating-point errors.

-1

Try is_integer() method:

x.is_integer()
Emma
  • 27,428
  • 11
  • 44
  • 69
Jung-suk
  • 172
  • 2
  • 12
  • 2
    This assumes that `x` is a float to begin with... What if it's a string? What if it's already an int? – Tomerikoo Aug 06 '20 at 14:25