[Tutor] Two Quick Questions

Allan Crooks allan.crooks@btinternet.com
Thu, 28 Jun 2001 00:17:23 +0100


Hi,

Don't know if you ever got an answer to this, so I'm answering it. :)

> My question is:  Does the difference in order of nots and variables make
> any difference when used in more complex, subtle ways?  If not, what is
> the usual way of ordering it?  I prefer the second beacuse it sounds
> nicer, but still ;o)

As far as I'm aware, it doesn't make the slightest bit of difference.

I assume that it was designed to prevent people accidently swapping the position of the not and getting the entire logic of the if statement reversed.

Should you want to test if the negative of an object is in a sequence (rather than if an object is not in a sequence), you just use brackets.

>>> L = [0, 1]
>>> S = 0
>>> if S not in L: print "Hello"
..
>>> if not S in L: print "Hello"
..
>>> if (not S) in L: print "Hello"
..
Hello
>>> if not S not in L: print "Hello"
..
Hello
>>>

I don't know why I did the last example. I was just seeing if it worked. It did, and it's a fairly frightening sentence to write, so let's forget all about double negatives. :)

Allan.