Check if object is a number or boolean

后端 未结 7 1054
攒了一身酷
攒了一身酷 2021-02-06 20:42

Design a logical expression equivalent to the following statement:

x is a list of three or five elements, the second element of which is

7条回答
  •  攒了一身酷
    2021-02-06 21:12

    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:

    • isinstance()
    • built-in numeric types

    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'
    

提交回复
热议问题