Definition
A program spends its time deciding: is this basket empty, has this user paid? Answering calls for a data type that knows only two answers and nothing in between. That type is the boolean, and False is its negative answer, True being the positive one.
False therefore stands for what is not the case: a comparison that does not hold, a box left unticked, a check that fails. It is a reserved keyword of the language, written with a capital letter and no quotes. Reserved means taken: the name belongs to Python, and you cannot reuse it as a variable name.
stock = 0
# A comparison never returns anything other than a boolean
print(stock > 10)
# False
# Here the value is written by hand, with no test involved
is_active = False
print(type(is_active))
# <class 'bool'>The first half of that example is the part to remember: every comparison produces one of those two values, and nothing else. So you never have to build a boolean in order to write a condition, the test does it for you, and Python runs the block only when the result does not come out false.
False, and everything that behaves like false
"Comes out false" hides a flexibility that catches beginners off guard. Python does not require an actual boolean inside a test: it accepts any value at all, then decides on its own whether that value counts as true or as false. Two things therefore have to be kept apart, the value False on one side, and on the other the values that are simply treated as false.
They are few enough to be learnt by heart. The table lists all of them, then closes with three values that are wrongly assumed to be false.
| Value | Inside a test |
|---|---|
False | False, it is the boolean itself |
None | False, but it means the absence of a value |
0 and 0.0 | False, the numeric zeros |
"" | False, the empty string |
[], {}, set() | False, the empty collections |
"0", [0], " " | True, because they are not empty |
That last row is the classic trap. The string "0" holds one character, so it is not empty, so it is true, even though reading it suggests the opposite.
In a form, data all arrives as strings. A field where the user typed 0 therefore reaches you as "0", which is true: the test if entry: lets it through as an ordinary value. Convert the entry before testing it, or a quantity of zero will be handled like any other quantity.
That flexibility is no flaw, though, and you will lean on it every day. It lets you write if results: rather than comparing to zero a length obtained with len. The test then reads as "if there are results", which is exactly the original intent.
False equals zero, literally
The boolean type is not a type apart: it derives from the int type. False therefore equals 0 and True equals 1, addition included. Far from being a language curiosity, this is a counting technique developers use every day.
The example below counts the correct answers of a quiz without writing a single loop.
answers = [True, False, True, True]
# sum() adds booleans up as ones and zeros
print(sum(answers))
# 3
print(False == 0)
# TrueThe downside shows up on the last line. Since False == 0 returns true, no plain equality will tell you whether you are holding a boolean or an integer. When the distinction matters, ask about the type rather than the value.
The equivalence holds for dictionary keys too. {0: "a", False: "b"} does not create two entries but a single one, worth "b": Python sees the same key there and overwrites the first.
Do not compare it explicitly
That leaves a matter of style, which quickly turns out to be a matter of correctness. Writing if valid == False: works, but no Python developer phrases things that way. The expected form goes through not, which flips the truth value of whatever it is given.
# Heavy, and only catches False exactly
if valid == False:
...
# Expected, and reads as "if valid does not hold"
if not valid:
...The flaw is not merely cosmetic. The comparison reacts to the value False and to nothing else, so it lets through an empty list, an empty string or a missing value, all of which should have been ruled out the same way. The not form leans on the rules of the table above and covers those three cases in one move.
As for is False, it compares identity rather than value. It is only justified in the rare case where a false boolean must be told apart from a zero, typically a function that returns sometimes 0, sometimes False, sometimes None.
Frequently asked questions
Why does lowercase false raise an error?
Because Python is case sensitive and only knows False with a capital letter. Written in lowercase, the word is no longer a keyword but an ordinary name that nothing ever defined, so execution stops on a NameError. People coming from JavaScript or PHP, where lowercase is the rule, hit this almost without exception.
What is the difference with an empty list inside a test?
None as far as the outcome goes, since both are false inside an if. The difference is one of meaning: False asserts a negative answer, whereas an empty list asserts that there is nothing to process. Mixing the two gets expensive as soon as a function can return either, because the caller no longer knows whether it received a failure or an empty result.
How can False be obtained from another value?
By calling bool() on that value, which applies exactly the rules of the table above: bool("") and bool([]) both return False. It is useful when storing an answer in a database, where a real boolean is wanted rather than an empty string, and it is also what Python does on its own at every test. The Python course covers these implicit conversions at length, as they produce the quietest bugs in the language.