Why can't you say "if True in first_cell:"

Screen Link: https://app.dataquest.io/m/349/project%3A-learn-and-install-jupyter-notebook/2/running-code

My Code:
welcome_message= (‘Hello, Jupyter’)
first_cell= True
if True in first_cell:
print(welcome_message)

What I expected to happen:
print the content of the variable welcome_message only if first_cell stores the boolean True.

What actually happened:
TypeError: argument of type ‘bool’ is not iterable

What the code suppose to be:
welcome_message= (‘Hello, Jupyter’)
first_cell= True
if first_cell:
print(welcome_message)

why can’t I use “if True in first_cell”? or
Why “first_cell” could refer to “if first_cell stores the boolean True”?

I know that this means (a boolen"=) in a (variable), which does not make sense, but why can it only say “if first_cell:”?

1 Like

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 -

if first_cell

which is simpler and straightforward to use and work with.

1 Like

in looks for the membership in given sequence by using __contains__ or iterating over iterator method internally.

See the details here

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:
    print(welcome_messaged)

Hello, Jupyter!

It will work as list has __contains__

first_cell.__contains__(True)
True

The in keyword has two purposes:

  • The in keyword is used to check if a value is present in a sequence (list, range, string etc.).

  • The in keyword is also used to iterate through a sequence in a for loop:

first_cell is a boolean, therefore you need to check if the True is same object as first_cell, in this case the is keyword is used.
So you have 3 methods to check if first_cell is true

  1. if first_cell - this will check if first_cell is True
  2. if first_cell is True - same as above it will check if first_cel is of the same object as True
  3. if first_cell == True - Use the == operator to test if two variables are equal.

The is keyword is used to test if two variables refer to the same object.

The test returns True if the two objects are the same object.

2 Likes