Booleans, None & type conversion
Use True/False and None, convert between types, and reason about truthiness.
True, False, None, and turning one type into another
Real programs live at boundaries where data arrives as text. A form field, a CSV cell, a query string, a JSON body from an API: all of it shows up as str, even when it means a number. Before you can add, compare, or store it you have to convert it, and you have to decide what "missing" looks like. Get the conversion or the missing-value check wrong and you either crash on bad input or silently treat empty data as real data. That is exactly the kind of edge case an interviewer builds a test around.
Booleans come from asking questions
A boolean is one of two values, True or False, and it is what a comparison hands back:
3 > 2 # True
3 == 4 # False (`==` compares; `=` assigns)
You use booleans to drive branches (if), loops (while), and filters. Keep == (compare) and = (assign) straight, because swapping them is a classic typo.
None means "there is nothing here"
None is Python's single "no value" object, used for "not set yet" or "no result". It is not 0 and not "", which are both real values. Test for it with identity, x is None, not x == None, because None is a unique singleton and is checks for that exact object.
Converting between types
Input often arrives as text, so convert it explicitly:
int("42") # 42 text -> integer
float("3.5") # 3.5 text -> float
str(42) # "42" number -> text
int() is strict. It parses "42" but raises ValueError on "", "3.5", or "12a". That strictness is why a function like parse_or_zero has to check for the empty string before it calls int(), not after.
Truthiness
In a condition, every value is either truthy or falsy. Memorise the falsy ones: False, None, 0, 0.0, "", [], {}, and (). Everything else is truthy.
Each of these sits alone in an if. Sort them by whether the block runs.
| Falsy value | Type | The truthy version |
|---|---|---|
| False | bool | True |
| None | NoneType | there is no truthy None |
| 0 | int | any non-zero int, including negatives |
| 0.0 | float | any non-zero float |
| '' (empty string) | str | any string with a character in it, even a single space |
| [] | list | any list with an item, even [0] |
| {} | dict | any dict with a pair |
| () | tuple | any tuple with an item |
"yes" if "hello" else "no" # "yes" non-empty string is truthy
"yes" if "" else "no" # "no" empty string is falsy
That A if condition else B shape is a conditional expression: it evaluates to A when the condition is truthy, otherwise B. It is the whole answer to a yes_no-style helper.
One trap: bool("False") is True and bool("0") is True, because both are non-empty strings. Truthiness asks whether the container is empty, not what the text spells. If you ever need to interpret the word "false", you must compare the string yourself.
Interview nuance: bool is a subclass of int in Python, so True equals 1 and False equals 0. That means sum([True, False, True]) is 2, a common one-liner for counting how many items pass a test. Interviewers probe this to see if you know isinstance(True, int) is True, and that a stray boolean can quietly do arithmetic instead of raising.
print(int("42") + 8) # 50
print(str(42) + "!") # 42!
print(3 > 2) # True
print("yes" if "" else "no") # no (empty string is falsy)Apply
Your turn
The task this lesson builds to.
Implement parse_or_zero(text): turn a string of digits into an integer, but return 0
when the string is empty.
For "42" return 42; for "" return 0.
3 hints and 4 automated checks are waiting in the workspace.
Practice
Make it stick
A second problem on the same idea, so it survives past today.
Implement yes_no(value): return the string "yes" when value is truthy, otherwise
"no".
Remember the falsy values: 0, "", None, and False.
2 hints and 5 automated checks are waiting in the workspace.