72

Design a logical expression equivalent to the following statement:

x is a list of three or five elements, the second element of which is the string 'Hip' and the first of which is not a number or Boolean.

What I have:

x = ['Head', 'Hip', 10]
print x[1] is 'Hip'

My question: How do you check for whether or not it is a Boolean or a number?

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
Joakim
  • 825
  • 1
  • 6
  • 6

8 Answers8

89

To answer the specific question:

isinstance(x[0], (int, float))

This checks if x[0] is an instance of any of the types in the tuple (int, float).

You can add bool in there, too, but it's not necessary, because bool is itself a subclass of int.

Doc reference:


To comment on your current code, you shouldn't rely on interning of short strings. You are supposed to compare strings with the == operator:

x[1] == 'Hip'
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
47

Easiest i would say:

type(x) == type(True)
CurlyMo
  • 479
  • 4
  • 3
37

In python3 this would be: type(x)==bool see example.

batman
  • 1,937
  • 2
  • 22
  • 41
Thijs Cobben
  • 529
  • 6
  • 10
18

Python 2

import types

x = False
print(type(x) == types.BooleanType) # True

Python 3

# No need to import types module
x = False
print(type(x) == bool) # True
Dev-I-J
  • 49
  • 1
  • 8
Peyman Karimi
  • 325
  • 2
  • 3
  • 1
    `types.BooleanType` was removed from Python 3. `bool` is used instead. Source: http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#types – KSR Aug 12 '16 at 21:53
6

You should compare the type of x to the bool class:

type(x) == bool

or:

type(x) == type(True)

Here is more on the type method

From Data model docs:

Booleans (bool)

These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

Community
  • 1
  • 1
Justin Golden
  • 69
  • 1
  • 1
1

I follow the recent answer who tell to use type and it seems to be the incorrect way according to pylint validation:

I got the message:

C0123: Using type() instead of isinstance() for a typecheck. (unidiomatic-typecheck)

Even if it's an old answer, the correct one is the accepted answer of @Lev Levitsky:

isinstance(x[0], (int, float))
Michael
  • 1,063
  • 14
  • 32
  • pylint cannot know what you want to do. There is a difference between type() and isinstance() because isinstance() will also consider sub classes and type() will not. How can pylint know which one you need in your program? Check out this article on the differences: https://switowski.com/blog/type-vs-isinstance –  Sep 23 '22 at 03:34
0

Python 3:

Use assert to check is some statement is True or False. If False, it will raise an AssertionError.

assert(isinstance(x[0], (int, float)))

You can then catch the error like that:

except AssertionError as error:
    print(error)
    # do stuff...
Onyr
  • 769
  • 5
  • 21
-1

I like to keep it simple to read.. This will accept bool, string, number and read it to a bool

def var2bool(v):
if type(v) == type(True):
    res = v
elif type(v) == type(0):
    if v == 0:
        res = False
    else:
        res = True
elif v.lower() in ("yes", "true", "t", "1"):
    res = True
else:
    res = False
return res
James Gardiner
  • 382
  • 2
  • 8