2

I am trying to figure out the most compact and pythonic way of checking if a variable is a positive integer. This is what I've tried so far.

a = None
b = 3.4
c = -1
d = 10
e = -5.7
f = '7'
g = [9]
h = {7}
i = 3j
j = r'8'
k = True
l = False

varlist = [a, b, c, d, e, f, g, h, i, j , k, l]

for vv in varlist:
    print( isinstance(vv, int) )

Current Output

False
False
True
True
False
False
False
False
False
False
True
True

Ideal Output

False
False
False
True
False
False
False
False
False
False
False
False
SantoshGupta7
  • 5,607
  • 14
  • 58
  • 116

2 Answers2

2

If you want exclusively int objects and no subtypes:

type(vv) is int and vv > 0:
    do_stuff()
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
1
for vv in varlist:
    print( type(vv) == type(0) and vv > 0 )
Maxxik CZ
  • 314
  • 2
  • 9