Course topics

By WebNest Studio

Python Tutorial

Booleans and None in Python

Every decision a program makes comes down to True or False. Python's bool type, the idea of truthiness (which values count as true or false), and the special None value that means "nothing here" appear in almost every line of real code.

This lesson covers Boolean values and operators, truthy and falsy values, short-circuit evaluation, and how to use and test for None correctly.

The bool Type

bool has exactly two values, True and False. Comparisons (==, <, in...) produce Booleans, and and, or and not combine them. bool is a subclass of int: True == 1 and False == 0, which is why sum() can count True values.

Truthy and Falsy Values

In an if or while, any value can be tested. Falsy values are: False, None, 0, 0.0, empty strings, lists, tuples, dicts and sets. Everything else is truthy. So if items: means "if the list is not empty".

Short-Circuit Evaluation

and stops at the first falsy operand and or stops at the first truthy one, and they return that operand itself, not necessarily a bool. This enables idioms like name = user_input or "Guest" and safe checks like if user and user.is_admin.

None

None is the single object of type NoneType, representing "no value" — a missing result, an optional argument not given, a function that returns nothing. Test for it with is None / is not None, not ==, and be careful not to confuse it with falsy values like 0 or empty strings.

Examples

Booleans, comparisons and bool as int

Python
age = 20
print(age >= 18, age == 21, "py" in "python")
print(True and False, True or False, not True)
print(True + True, isinstance(True, int))

marks = [45, 78, 90, 32, 66]
print("Passed:", sum(m >= 40 for m in marks))
Output
True False True
False True False
2 True
Passed: 4

Truthy and falsy values

Python
values = [0, 7, "", "hi", [], [0], {}, None, 0.0, " "]
for v in values:
    print(f"{v!r:6} -> {bool(v)}")
Output
0      -> False
7      -> True
''     -> False
'hi'   -> True
[]     -> False
[0]    -> True
{}     -> False
None   -> False
0.0    -> False
' '    -> True

Short-circuit idioms and None checks

Python
def find_user(email):
    users = {"asha@webnest.in": "Asha"}
    return users.get(email)          # None when not found

nickname = "" or "Guest"
print(nickname)

user = find_user("ravi@webnest.in")
if user is None:
    print("No such user")

count = 0
print("count is None?", count is None, "| count falsy?", not count)
print(None or 0 or "fallback")
Output
Guest
No such user
count is None? False | count falsy? True
fallback

Common Mistakes

  • Writing if x == None instead of if x is None.
  • Using "if value:" when 0 or "" are valid values, accidentally treating them as missing.
  • Writing true/false in lowercase.
  • Expecting and/or to always return True/False — they return one of the operands.

Key Points to Remember

  • bool has two values and is a subclass of int (True == 1).
  • Falsy: False, None, 0, 0.0, and empty collections/strings; everything else is truthy.
  • and/or short-circuit and return an operand; "x or default" is a common idiom.
  • Test None with is / is not.

Practice the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.