Python BasicsBeginner7 min10 / 63

Booleans & None

Learn how Python represents yes/no decisions with True and False, discover the special None value, and understand why some values secretly act like false.

Every program needs to make decisions. "Is the user logged in? Is the list empty? Did the search find anything?" To answer questions like these, Python has a special type called a boolean — a value that is either True or False. No in-between, no maybes. Just those two.

See it in action

Visual walkthrough1 / 6
1

Python's Yes/No Answer

Every program needs to make decisions. Python uses booleans — values that are either True or False — to represent those yes/no answers.

Booleans are named after mathematician George Boole, who formalized logic with just two states.

#Meet True and False

In Python, booleans are written with a capital first letter: True and False. This matters — true (all lowercase) is not the same thing and will cause an error. Think of them as Python's way of saying "yes" and "no".

Booleans are their own type in Python, called bool.
is_raining = True
is_sunny = False

print(is_raining)
print(is_sunny)
print(type(is_raining))
Common mistake

Capitalization matters!

Python is case-sensitive. True works, but true will throw a NameError. Always capitalize the T and F when writing boolean values.

#Comparisons Produce Booleans

You rarely type True or False directly. Instead, you write a comparison, and Python figures out the boolean for you. Comparisons use operators like == (equal to), != (not equal), >, <, >=, and <=.

Each comparison evaluates to either True or False.
age = 20

print(age == 20)   # Is age exactly 20?
print(age > 18)    # Is age greater than 18?
print(age < 18)    # Is age less than 18?
print(age != 30)   # Is age not equal to 30?
Think of it like

== vs =

Think of = as putting something in a box (assignment), and == as asking "are these the same?" (comparison). age = 20 stores the number. age == 20 asks a yes-or-no question.

#Truthy and Falsy Values

Here is something surprising: every value in Python can act like a boolean, even if it is not actually True or False. Python calls this being truthy or falsy. When Python needs a yes/no answer, it will treat certain values as false automatically.

Values that are falsy (act like False): - False itself - 0 (the number zero) - "" (an empty string) - [] (an empty list) - {} (an empty dictionary) - None (more on this shortly)

Pretty much everything else is truthy — a non-zero number, a non-empty string, a list with items, etc.

bool() converts any value to its True/False equivalent.
print(bool(0))      # zero is falsy
print(bool(42))     # non-zero is truthy
print(bool(""))     # empty string is falsy
print(bool("hi"))   # non-empty string is truthy
print(bool([]))     # empty list is falsy
print(bool([1, 2])) # non-empty list is truthy
Tip

Why does this matter?

Truthy and falsy values let you write shorter, more readable conditions. Instead of if len(my_list) > 0: you can just write if my_list: — Python handles the check for you.

#None: The Absence of a Value

None is Python's way of saying "there is nothing here". It is not zero, not an empty string — it is the complete absence of a value. You will see it when a function does not explicitly return anything, or when you want to signal that a variable has not been set yet.

None has its own special type called NoneType.
result = None
print(result)
print(type(result))
Functions that don't return a value implicitly return None.
def greet(name):
    print("Hello, " + name)

return_value = greet("Ada")
print(return_value)

#Checking for None with `is`

To check if a variable is None, use the keyword is rather than ==. This is the Python convention, and it is more precise. is checks that two things are the exact same object in memory — and since there is only ever one None in Python, this is the right tool.

Use `is None` to check for the absence of a value.
user_input = None

if user_input is None:
    print("No input provided yet.")
else:
    print("Got input:", user_input)
Watch out

Avoid == None

While value == None often works, the Python community strongly prefers value is None. Some custom objects can override == in unexpected ways, making is None safer and clearer.

#Booleans Are Really 1 and 0

Here is a fun peek under the hood: Python's True and False are actually secretly the numbers 1 and 0. This means you can do arithmetic with them! It is not something you do every day, but it explains a lot about how Python works internally.

True equals 1 and False equals 0 under the hood.
print(True + True)   # 1 + 1
print(True + False)  # 1 + 0
print(False * 100)   # 0 * 100
print(int(True))     # converts to integer
print(int(False))
Note

bool is a subtype of int

Technically, bool inherits from int in Python. That is why isinstance(True, int) returns True. This is a quirk of Python's history — but in everyday code, treat True and False as booleans, not numbers.

Quick check

What does `bool("")` return?

Key takeaways

  • `True` and `False` must be capitalized — `true` and `false` are not valid Python.
  • Comparisons like `==`, `>`, and `!=` always produce a boolean result.
  • Many values are truthy or falsy — `0`, `""`, `[]`, `{}`, and `None` all act like `False`.
  • `None` means "no value" and should be checked with `is None`, not `== None`.
  • Under the hood, `True` is `1` and `False` is `0` — booleans are a special kind of integer.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
print(bool(""))
print(bool("hello"))
print(bool(0))
print(bool(1))
Fix the bug#2

This code has a bug. What is wrong?

fix-bug
is_active = true
print(is_active)
Fill in the blank#3

Complete the code so it checks whether `score` is None using the recommended Python convention.

score = None

if score  None:
    print("No score yet.")
Reorder the lines#4

Put these lines in the right order to print the result of a comparison and its type.

1
age = 17
2
print(result)
3
result = age >= 18
4
print(type(result))
Your turn
Practice exercise

Write a function called describe_value that takes any single argument and prints: "Truthy" if the value is truthy, or "Falsy" if it is falsy. Then call it with at least four different values — try things like 0, "hello", [], and None — and observe the output.

Try it live — edit the code and hit Run to execute real Python:

solution.py · editable