Skip to main content

Booleans, None & type conversion

Level 1: Level 1: Foundationseasy9 minbooleansnonetype-conversiontruthiness

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
Check yourself
A form field arrives empty and your code calls int(text) on it. What happens?

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.

Check yourself

Each of these sits alone in an if. Sort them by whether the block runs.

0
0.0
An empty string
A string holding a single space
The string '0'
An empty list
The list [0]
None
Table
Every falsy value is empty or zero. Two traps live in the right column: a single space is a non-empty string and therefore truthy, and [0] is a list containing a falsy item and is itself truthy. Emptiness is about the container, never its contents.
Falsy valueTypeThe truthy version
FalseboolTrue
NoneNoneTypethere is no truthy None
0intany non-zero int, including negatives
0.0floatany non-zero float
'' (empty string)strany string with a character in it, even a single space
[]listany list with an item, even [0]
{}dictany dict with a pair
()tupleany tuple with an item
Every falsy value is empty or zero. Two traps live in the right column: a single space is a non-empty string and therefore truthy, and [0] is a list containing a falsy item and is itself truthy. Emptiness is about the container, never its contents.
"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.

Check yourself
A config file hands you the text False as a string. What does bool('False') return?

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.

Check yourself
What does sum([True, False, True]) return?

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.

Worked example (Python)
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.