Python Booleans and None
What you will learn
- The boolean values
TrueandFalse - How comparisons produce booleans
- Why
True + Trueis2 - A first look at truthiness:
bool(0),bool("") - What
Noneis, and how to test for it withis None
1. Booleans
A boolean (type bool) has only two possible values: True and False. They are used for yes-or-no answers, and they are what if statements (chapter 5) look at.
print(True, False, type(True))
Output:
True False <class 'bool'>
Write them exactly with a capital first letter. true and false are not Python.
print(true)
NameError: name 'true' is not defined
Quick check: is `False` the same as the text `"False"`?
No. False is a boolean value. "False" is text, with quotation marks.
2. Comparisons give booleans
A comparison asks a question and answers it with a boolean:
print(5 > 3, 2 == 3, "a" == "a")
True False True
Notice the double equals: == asks "are these equal?", while a single = stores a value in a name (lesson 2.1). Chapter 5 covers all the comparison operators in detail.
3. Booleans are also numbers
In Python, True counts as 1 and False as 0. A bool is a special kind of int:
print(True + True, True * 5, False + 1)
print(int(True), int(False))
2 5 1
1 0
This is useful for counting: adding up several comparisons counts how many were true. It is also the reason isinstance(True, int) is True (lesson 2.2).
4. A first look at truthiness
Any value can be turned into a boolean with bool(...). Most values give True. A few "empty" or "zero" values give False:
print(bool(0), bool(7), bool(""), bool("a"), bool(None), bool([]), bool([0]))
False True False True False False True
Gives False |
Gives True |
|---|---|
0 (and 0.0) |
any other number |
"" (empty text) |
any text with something in it |
[] (empty list) |
a list with items, even [0] |
None |
most other values |
Python uses this in if statements: an empty value counts as "no", and anything else counts as "yes". You will use that fully in lesson 5.3.
5. None: the value for "nothing here"
None is a special value that means "no value". Its type is NoneType, and there is only one None.
x = None
print(x, type(x), x is None)
None <class 'NoneType'> True
You use None when a name needs to exist but has no real value yet:
name = None
if name is None:
print("no name yet")
no name yet
Testing for None: use is
To check for None, write is None (and is not None for the opposite). This is the standard Python habit. None == None also works, but is says exactly what you mean: "is this the one and only None?"
Careful: None is not zero
print(None == 0, None == False, None is None)
False False True
None is not 0, not False and not empty text. Although bool(None) is False, None is its own value.
Functions that give back nothing
A function that does not give a result back actually gives None. You can see this with print itself:
result = print("hi")
print(result)
hi
None
print shows text on the screen, and its own result is None. This matters when you write functions (chapter 11).
Worked example: counting passes with booleans
Marks are 70, 45, 88 and 52. How many are at least 50?
marks = [70, 45, 88, 52]
passed = (70 >= 50) + (45 >= 50) + (88 >= 50) + (52 >= 50)
print(passed)
| Comparison | Result | Counts as |
|---|---|---|
70 >= 50 |
True |
1 |
45 >= 50 |
False |
0 |
88 >= 50 |
True |
1 |
52 >= 50 |
True |
1 |
The sum is 3, so 3 marks are passes. (In chapter 6 a loop will do this for any number of marks.)
Common mistakes
- Writing
true,falseornonein lowercase. Python needsTrue,FalseandNone. - Using
=instead of==to compare. - Putting quotation marks around them.
"True"is text, not a boolean. - Testing
Nonewith== 0or by assuming it is empty text. Useis None. - Expecting
print()to give back what it shows. Its result isNone.
Key takeaways
- A
boolisTrueorFalse(capital first letter). - Comparisons such as
5 > 3give booleans;==compares and=stores. Truecounts as1andFalseas0.bool(x)shows whether a value counts as true:0,"",[]andNoneare false.Nonemeans "no value". Test for it withis None.
Practice
1. What does print(10 == 10.0) show?
Show answer
True. An int and a float with the same value are equal.
2. What does True + 2 give?
Show answer
3, because True counts as 1.
3. What are bool("") and bool(" ") (a single space)?
Show answer
False and True. The empty text is false, but a space is a character, so the text is not empty.
4. Write one line that checks whether a name answer holds None.
Show answer
answer is None.
5. What is the error in x = 5 followed by if x = 5: as a comparison? What should it be?
Show answer
A single = stores a value, so it cannot be used to compare. The comparison is x == 5.
6. What does print(print("a")) show?
Show answer
Two lines: a, then None. The inner print shows a and returns None, which the outer print then shows.