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?
To answer the specific question:
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, becausebool
is itself a subclass ofint
.Doc reference:
isinstance()
To comment on your current code, you shouldn't rely on interning of short strings. You are supposed to compare strings with the
==
operator:You should compare the type of
x
to thebool
class:or:
Here is more on the type method
From Data model docs:
In python3 this would be:
type(x)==bool
see example.I follow the recent answer who tell to use
type
and it seems to be the incorrect way according topylint
validation:I got the message:
Even if it's an old answer, the correct one is the accepted answer of @Lev Levitsky:
Easiest i would say: