How do I know if the variable is an integer in Python?

Asked

Viewed 23,915 times

8

How can I know if the variable is an integer in Python?

I know how to do it in PHP:

is_int($variable); // bool(true)

But how can I do in Python?

Example:

numero = 1 #é numero
nao_eh_numero = 'texto' # não é número

5 answers

13


Valid for Python 2 and 3

Using isinstance

Pass as first parameter the variable you want to validate and as second parameter the "type".

Example:

isinstance(numero, int) # True

Observing

If you intend to validate if the variable is a number, using Python 2. You will need to change the second parameter to (int, long), in Python 3 this is not necessary because it does not exist long.

isinstance(numero, (int, long))

Using type

type(numero) == int # True

The difference between the two is that isinstance also validates whether the object is derived from the class (a daughter class). That is, it checks the entire inheritance structure of the object. Already the type returns only type exact.

Example to illustrate

class MinhaClasseInt(int):
    pass

x = MinhaClasseInt(0)

type(x) == int # False
isinstance(x, int) # True

0

I used this formula here which is easier and gives the exact value. No need to worry about the code consider the (type)! as:

x1 = float(input('Valor = '))
print(x1, type(x1))
def valor(v):
  n = int(v)
  m = v * 10
  s = n * 10
  if m == s:
    return ('O número {} é inteiro'.format(v))#retorn 1
  else:
    return ('O número {} é decimal'.format(v))#retorn 2 
r = valor(x1)
print(r)

This ai has how you change the (Return) or decrease and it will check if the value is integer or decimal (float "floating"])

You can also test the code and the result with this one:

import os
while True:
  x1 = float(input('Valor = '))
  print(x1, type(x1))
  def valor(v):
    n = int(v)
    m = v * 10
    s = n * 10
    if m == s:
      return ('O número {} é inteiro'.format(v))#retorn 1
    else:
      return ('O número {} é decimal'.format(v))#retorn 2 
  r = valor(x1)
  print(r)
  p = input('RECOMEÇAR ! Aperte [ENTER]')
  os.system('cls' if os.name == 'nt' else 'clear')

But the basic thing is this:

def valor(v):
    n = int(v)
    m = v * 10
    s = n * 10
    if m == s:
      return ('O número {} é inteiro'.format(v))#retorn 1
    else:
      return ('O número {} é decimal'.format(v))#retorn 2 

In the return part you have (#reorn 1) or (#retorn 2). It is to make it easier to deal with the results and to type like this:

def valor(v):
  n = int(v)
  m = v * 10
  s = n * 10
  if m == s:
    return 1
  else:
    return 2 
while True:
  n = float(input('valor = '))
  r = valor(n)
  #Agora vc pode brincar
  if r == 1:
    print('Você digitou um numero INTEIRO se imbecil !')
  elif r == 2: # Ou vc pode usar [ELSE]
    print('Você digitou um numero DECIMAL seu aderbal !')

There it is more or less like this:

inserir a descrição da imagem aqui

-1

I’d do it different and easier. Do so:

numero = float(input('Digite um numero qualquer :'))

if(numero // 1 == numero): 
    print('\nNúmero inteiro !')
else:
    print('\nNúmero Decimal !')

Note that number 1 returns only the whole part; therefore, if that division returns the number itself then it is an integer.

  • 1

    It is the same thing that William BL replied: https://answall.com/a/228700/3635

-1

Actually this // doesn’t even need to be done, just ask:

if int(numero) == numero:
  • But the other reply I already said that. :/

-3

But using both the type as to the isinstance, if the content of the variable is typed with a decimal point (12.0, for example), the variable will be a float and the result of the expression will be False, even if the number is integer.

I think this answer gets better:

if numero // 1 = numero: # se for True o número é inteiro

This works even if it is a type variable float.

  • 1

    If it is a float, it is not an integer. Mathematically they can even be equivalent, but computationally not. The float type requires a different representation, usually following the pattern defined in IEEE 754.

  • Yes, the computational answer is with the code above. But I wanted to improve and think about an answer to the mathematical question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.