Python Part I · Module 2 · Variables and Data Types
True, False, and Nothing at All
Numbers hold quantities. This chapter covers the values that hold something else: a decision, and the absence of one. True and False record a verdict — did this run pass, was the file found — and they are values in their own right rather than text that happens to spell a word. None is subtler and more useful than it first appears: it marks a place where there is deliberately no answer yet, which is a genuinely different statement from zero or empty. Getting that distinction right is what later stops a script reporting a run it never managed to read as a run that came back clean, and that is a bug worth never writing.
Foundation9 min readPythonPart IFundamentalsboolNone
Part I · Module 2 · Chapter 2.8 · True, False, and Nothing at All
1. Recording a Verdict
Some facts are not quantities. Did the run pass? has two possible answers and neither is a number.
passed = True
timed_out = False
print(passed)
print(timed_out)True
FalseTrue and False are values, exactly as 4210 is a value. Python calls this type bool, short for boolean.
There are only two of them. That is the entire type.
2. The Capitalisation Is Part of the Value
Chapter 2.1 §4 flagged this and it is worth repeating once, because it is the most common first-week error for anyone arriving from another language.
passed = trueNameError: name 'true' is not defined. Did you mean: 'True'?Python looked for something named true and found nothing — the NameError from Chapter 1.11, being helpful about the fix.
Capital T, capital F. Not true, not TRUE, not "true".
3. Naming a Boolean
A small habit that pays off later. Because a boolean answers a yes-or-no question, it reads best when its name is that question:
passed = True
timed_out = False
has_waveform = FalseEach of those reads as a statement when true. Compare with a name like status = True, which leaves the reader asking the status of what, and what does true mean here?
You will see the full value of this in Module 9, where these names appear inside decisions and the code reads almost as English.
4. True Is Not "True"
This is the distinction that matters most, and it is worth a moment even before strings are formally introduced in the next chapter.
a = True
b = "True"
print(a)
print(b)True
TrueIdentical output. Different values.
True is a boolean — one of the two verdict values. "True" is text containing the four characters T, r, u, e, and Python treats it exactly as it would treat "axi_smoke".
This is Chapter 2.1 §3 again: what a value looks like when printed does not tell you what kind of value it is. Chapter 2.9 introduces text properly, Chapter 2.10 gives you a way to ask Python which you are holding, and Chapter 2.11 explains why a value read from a file is almost always the text one.
5. When There Is No Answer
Now the second half of the chapter.
Suppose a script is going to record which line a run first failed on. Before it has looked, what should that name refer to?
Not 0 — that would claim it failed on line zero. Not "" — that would claim there is a message and it is empty. Not False — that would claim it did not fail.
Python provides a value for exactly this:
first_failure = None
print(first_failure)NoneNone means there is deliberately no meaningful value here. Python calls its type NoneType, and None is the only value of that type.
6. What None Is Not
Four corrections, because each one is a belief that produces a real bug.
Noneis not zero. Zero is a count, and counting zero errors is a result.Nonemeans nobody counted.Noneis notFalse.Falseis a verdict.Noneis the absence of one.Noneis not empty text. Empty text is a message that happens to have no characters.Noneis the absence of a message.Noneis not uninitialised memory or a null pointer. It is an ordinary, specific value that you can assign, print and pass around deliberately.
7. None Is Not "None"
The same trap as §4, for the same reason:
a = None
b = "None"
print(a)
print(b)None
NoneOne is the absence value. The other is four characters of text. They print identically and behave completely differently.
8. A Note on What Is Deferred
You may be wondering whether None counts as false when a program makes a decision, or whether zero does.
It does, and there are rules for it — but decisions themselves are Module 9 — Conditional Statements, and learning the rules for how values behave inside a decision before you have met a decision is the wrong order.
For now, treat True and False as verdicts you record, and None as a marker for no answer yet. That is complete and correct for everything in Module 2.
9. Predict, Run, Observe
Predict the full output before running:
passed = True
failed = False
reason = None
print(passed)
print(failed)
print(reason)
reason = "timeout"
print(reason)True
False
None
timeoutThe last two lines are ordinary reassignment from Chapter 2.3 — reason stopped referring to None and started referring to a piece of text. That is exactly the pattern a real script uses: start at None, and replace it once there is an actual answer.
10. Exercise
Exercise 1 — three verdicts. Write a file recording whether a run passed, whether it timed out, and whether waveforms were captured. Use names that read as questions. Print all three.
Exercise 2 — predict the error. Predict what each does, then run them:
a = True
b = true
c = TRUETwo fail. Name the error and say what Python was looking for in each case.
Exercise 3 — zero or None. For each situation say whether you would use 0, None, or False, and why in a few words:
(a) a run completed with no errors; (b) the log file could not be opened, so nothing is known; (c) waveform capture was switched off; (d) the first failing line, before the log has been read.
Exercise 4 — the dangerous pair. Explain in two sentences why a script that uses 0 for both "no errors found" and "could not check" is dangerous. What would it eventually report, and to whom?
11. Summary
TrueandFalseare the two boolean values, typebool. They record a verdict, not a quantity.- Capitalisation is mandatory. Lowercase
trueis aNameError; other languages spell it differently and Python does not follow them. - Name a boolean as the question it answers —
passed,timed_out— so later code reads as a statement. Nonemarks a deliberate absence of value, typeNoneType.Noneis not zero, notFalse, not empty text, and not a null pointer. Each of those confusions produces a real bug.Trueis not"True", andNoneis not"None". The printed output is identical; the values are not.- The starting-at-
None-then-replacing pattern is how real scripts record an answer they do not have yet.
Text has now been referred to three times without being properly introduced. The next chapter fixes that.
Where this fits
Part of the Python curriculum.
