0

In Python is it possible for me to code this and have it work as intended?

if a != None and b == None or a == None and b != None

basically I want to see if either a or b is None while the other isn't

1
  • 1
    Side note, the "correct" way of comparing to None is usually using is, i.e. ... a is not None and b is None .... Other than that your code should work as intended. Commented May 21, 2014 at 4:02

2 Answers 2

8

Since it sounds like you want an xor...

if (a is None) != (b is None):
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

+1 You can write that as if (a is None) is not (b is None):
1

In Python, there can be only one instance of NoneType can exist, so you can use is operator, like this

if (a is None and b is not None) or (b is None and a is not None):

You can also count the number of Nones like this

if (a, b).count(None) == 1:

Or you can explicitly check that like this

if a != b and (a is None or b is None):

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.