Simply put, because that’s how the programming language was designed.
Your first_cell is a boolean data type (or rather, it stores/points to a boolean object). And boolean data types in Python are not iterable. So, the in does not work for them and throws the error as you can see.
For boolean checks the alternative could be to use is -
if first_cell is True:
or, the more “clunky” one and not usually preferred -
if True is first_cell:
But because of how Python is designed, the above are equivalent to -
which is simpler and straightforward to use and work with.
For user-defined classes which define the __contains__() method, x in y returns True if y.__contains__(x) returns a true value, and False otherwise.
For user-defined classes which do not define __contains__() but do define __iter__(), x in y is True if some value z , for which the expression x is z or x == z is true, is produced while iterating over y . If an exception is raised during the iteration, it is as if in raised that exception.
That means to use in it must be iterable or must has __contains__ method.
Hence, You are using bool type that does not have __contains__ method and that is not either iterable. So it giving exception.
For example you make it list instead of bool. - first_cell = [True]
welcome_messaged = 'Hello, Jupyter!'
first_cell = [True]
if True in first_cell: