0

I have a simple logic if statement returning an invalid syntax error. The statement is:

if (a[1] != None and a[2] != None and !(a[3] == None and a[4] == None)):

The invalid syntax is the third ! operator. Any reason why this would not work? Is there another operator I am supposed to use in this situation?

So the logic is essentially: a[1] ^ a[2] ^ (a[3] v a[4]) (where these denote having values). Hence the reverse logic for getting no None values is:

!a[1] ^ !a[2] ^ !(a[3] ^ a[4])

I'm pretty sure my logic maths is right, so how do I get the result I require?

*Background info: Python 2.7.10, overall code is pulling data out of an SQL Server 2008 table, manipulating it and then inserting it into a different table that doesn't allow NULL values, and the original table is littered with NULLs

Thanks for your help!

Aaron Troeger
  • 165
  • 1
  • 5
  • 18

2 Answers2

6

The logical not operator in python is not ! but rather is not.

You want:

if (a[1] != None and a[2] != None and not (a[3] == None and a[4] == None)):
Gort the Robot
  • 2,329
  • 16
  • 21
2

Well, stylistically, when using None it is preferred to use is and is not rather than == and !=, so

if (a[1] is not None and a[2] is not None) and not (a[3] is None and a[4] is None):
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51