10/19: Code style guideline: NOT IN (Just being pedantic!)


I am working my way through this code (thanks heaps CE!).

With regards to the following line of code in this exercise, I propose it should be changed, from (currently):

if not product in self.items_in_cart:

to this:

if product not in self.items_in_cart:

Two reasons I suggest this:

  • Firstly is that it is simply because I (as a novice programmer) had trouble deciphering what this line of code was actually doing when I first read it, as its written (as the first option) - so I rewrote it (as the second option) and the code still ran correctly, and I could understand it easier too. :smile:

  • Secondly, the second option seems to conform to the official Python specifications more closely.

e.g. from: https://www.python.org/dev/peps/pep-0008/

"Use is not operator rather than not ... is."
While both expressions are functionally identical, the former is more readable and preferred.
Good: if foo is not None:
Bad: if not foo is None:

(I extrapolated this "not is" example to our "not in" example).

I understand the computer will (correctly) read them as being equal (due to operator implementation hierarchy) so its just a convention and human user friendliness that drives my suggestion.

I am interested to hear from any less novice programmers about this suggestion (and also more generally perhaps, outside of this specific example).

Once again; huge thanks to all for any help and suggestions.



I will have to concur with your use of not .

Here is an example of the use of not for you though.

truth = False

if not truth:

When you do not have comparison for the if statement it automagically compares it to True. Using that we can use short statements like the one I provided.


Hi Zeziba,

Thanks for your reply.

Regarding your example: just to clarify the assumption of 'truth' in if statements:

For example: Another quote from the same link I put above (Python Style Guide):

" For sequences, (strings, lists, tuples), use the fact that empty sequences are false."

So how I interpret that (correctly or incorrectly) is there are cases where we need to be careful about assuming this 'truth' (in the cases of empty sequences).

(So I wouldn't say it 'automatically compares it to True'.) Unless I am understanding this incorrectly? (I haven't tried to run an example code yet..). But I think it supports your style anyway: I'm just saying it won't always be true and us learners might need to be careful of that!..



To test if what I say is true just do the following,

if True:


if False:

After you preform this experiment you will see that if you leave the comparative side empty it will always compare it to True. If the statement evaluates to False it will move on.


Yeah that makes sense. I mis-interpreted the Style Guide recommendation thats says:

"For sequences, (strings, lists, tuples), use the fact that empty sequences are false."

To mean:

x = ["", "", ""]
if x is False:
    print (True)

(When in fact its neither True nor False..)



To test if something is True/False just compare it.

Empty objects no matter what it is comes out False because it is a 0 which is False in python.


lista = []
dicta = {}
stringa = ''

Then compare them to a literal

lista == False
dicta == False
stringa == False

After each input it should give you a true or false based on your question.(FYI these are all True, meaning that the object is empty)

If you put empty objects into a list the list is therefore not empty.


x = ["", "", ""]


Thanks again zeziba.

I think there is something wrong with my codecaedy interface then.

Note: to test these things I just type them into the interface of whatever exercise I am working on.

Before (e.g. my post/reply to you): every time I tested any of these they all came out as "None", now they all come out as "False".

x = []
x = ""
x = "1"
x = "abc"
x = ["",""]
x = ["a","b"]

tested under:

if x is True:
    print "True"
    print "False"

(now they all come out as "False")

  • maybe the specific example interfaces have something going on in the background which causes a bug sometimes?



That is possible also sometimes there are incompatibilities between browsers and the course too.


Oh ok - I am using the Firefox Developer browser (v43.a02). (Hoping that since its 64-bit it might be faster than the usual 32-bit Firefox...). Maybe there is some compatibility issue (no doubt an ongoing battle with anyone maintaining websites! :smile: ).