False or None vs. None or False

Solution 1:

The expression x or y evaluates to x if x is true, or y if x is false.

Note that "true" and "false" in the above sentence are talking about "truthiness", not the fixed values True and False. Something that is "true" makes an if statement succeed; something that's "false" makes it fail. "false" values include False, None, 0 and [] (an empty list).

Solution 2:

The or operator returns the value of its first operand, if that value is true in the Pythonic boolean sense (aka its "truthiness"), otherwise it returns the value of its second operand, whatever it happens to be. See the subsection titled Boolean operations in the section on Expressions in the current online documentation.

In both your examples, the first operand is considered false, so the value of the second one becomes the result of evaluating the expression.

Solution 3:

EXPLANATION

You must realize that None, False, and True are all singletons.

For example, if foo is not None means that foo has a value other than None. This works the same as just having if foo, which is basically if foo == True.

So, not None and True work the same way, as well as None and False.

>>> foo = not None
>>> bool(foo)
True
>>> foo = 5  # Giving an arbitrary value here
>>> bool(foo)
True

>>> foo = None
>>> bool(foo)
False
>>> foo = 5  # Giving an arbitrary value here
>>> bool(foo)
True

The critical thing to realize and be aware of when coding is that when comparing two things, None needs is, but True and False need ==. Avoid if foo == None and only do if foo is None. Also, avoid if foo != None and only do if foo is not None.

WARNING

If you use if foo or if not foo, beware of potential bugs. So, don't use that at all and be on the safe side. It is very important to follow best practices shared by Python experts.

REMINDER

True is a 1, and False is a 0.

In the old days of Python, we only had an integer 1 to represent a truthy value, and we had an integer 0 to represent a falsy value. However, it's more understandable and human-friendly to say True instead of 1, and more understandable and human-friendly to say False instead of 0.

Solution 4:

A closely related topic: Python's or and and short-circuit. In a logical or operation, if any argument is true, then the whole thing will be true and nothing else needs to be evaluated; Python promptly returns that "true" value. If it finishes and nothing was true, it returns the last argument it handled, which will be a "false" value.

and is the opposite, if it sees any false values, it will promptly exit with that "false" value, or if it gets through it all, returns the final "true" value.

>>> 1 or 2 # first value TRUE, second value doesn't matter
1
>>> 1 and 2 # first value TRUE, second value might matter
2
>>> 0 or 0.0 # first value FALSE, second value might matter
0.0
>>> 0 and 0.0 # first value FALSE, second value doesn't matter
0

Solution 5:

From a boolean point of view they both behave the same, both return a value that evaluates to false.

or just "reuses" the values that it is given, returning the left one if that was true and the right one otherwise.