Python Boolean Values
Table of Contents + −
In the last lesson you learned about Strings in Python, where text lives inside quotes. Now we move to a much smaller kind of value. This one holds only a yes or a no. It sounds tiny. But this little value is what every decision in your program rests on.
🤔 Why Do We Need Booleans?
Think about an online shopping cart. Before the app shows the “Pay Now” button, it has to answer one question. Is the cart empty or not? That answer is not a number. It is not a sentence either. It is just yes or no.
A program needs a clean way to store that yes-or-no answer so it can act on it. That is exactly what a boolean does. A boolean is a value that can only be one of two things. It is either True or False.
Once you have a True or False sitting in your program, you can branch on it. You show one thing when it is True. You show another thing when it is False.
🧩 What a Boolean Looks Like
A boolean has only two possible values. In Python they are written with a capital first letter:
is_logged_in = Truecart_is_empty = False
print(is_logged_in)print(cart_is_empty)This prints the two values back to us, exactly as written.
Output
TrueFalseThe capital letter matters a lot here. True and False are special Python words. If you type them in lowercase, Python does not know what you mean.
# ❌ Avoid: lowercase is not a Python booleanis_ready = true
# ✅ Good: capital first letteris_ready = TrueThe first line crashes with a NameError. Python looks for something named true and finds nothing. So always remember the capital T and capital F.
Tip
A common habit is to name boolean variables like a question that has a yes or no answer. Names like is_empty, has_account, and is_admin work well. When you read the code later, it almost sounds like a real sentence.
🧮 Where Booleans Come From
You will not type True and False by hand very often. Most of the time, Python makes a boolean for you when you compare two things.
When you ask “is this bigger than that?”, Python checks and hands you back a boolean as the answer:
print(5 > 3)print(2 > 10)print(10 == 10)Here > means greater than and == means “is equal to”. Python works out each comparison and gives you the True or False result.
Output
TrueFalseTrueThese comparison signs are the everyday tools you will use to create booleans:
| Operator | Means | Example | Result |
|---|---|---|---|
== | is equal to | 7 == 7 | True |
!= | is not equal to | 7 != 3 | True |
> | greater than | 9 > 12 | False |
< | less than | 3 < 8 | True |
>= | greater than or equal to | 5 >= 5 | True |
<= | less than or equal to | 4 <= 2 | False |
Caution
One equals sign and two equals signs do different jobs. A single = puts a value into a variable. A double == asks whether two things are the same. Mixing these up is one of the most common early bugs. So watch for it.
🔁 Turning Other Values Into a Boolean
Sometimes you have a value that is not a boolean, like a number or some text. And you want to know its True or False meaning. Python gives you a tool for that called bool().
You hand bool() any value, and it hands you back either True or False:
print(bool(5))print(bool(0))print(bool("hello"))print(bool(""))Each line passes a value into bool() and prints the boolean that comes out.
Output
TrueFalseTrueFalseNotice the pattern. The number 5 and the text "hello" came back as True. But the number 0 and the empty text "" came back as False. That is not random. Python has a rule for which values count as True and which count as False.
🌗 Truthy and Falsy Values
Every value in Python has a hidden yes-or-no side. We call a value falsy when Python treats it as False. We call it truthy when Python treats it as True.
The falsy values are a short, fixed list worth remembering:
Falseitself0(the number zero)""(empty text)[](an empty list)None(the “nothing here” value)
Here is the short list in action. Each of these is treated as False:
print(bool(0))print(bool(""))print(bool([]))print(bool(None))All four are falsy, so each one prints False.
Output
FalseFalseFalseFalseAlmost everything else is truthy. Any non-zero number, any text with characters in it, and any list with items inside all count as True:
print(bool(42))print(bool("hi"))print(bool([1, 2, 3]))These all have something in them, so each one is truthy and prints True.
Output
TrueTrueTrueNote
A quick way to remember it is this. “Empty or zero or nothing” is falsy. “Has something in it” is truthy. An empty cart is falsy. A cart with items is truthy.
🛒 Using a Boolean in an if
A boolean is most useful when you use it to make a decision. The if statement runs a block of code only when its condition is True. And a boolean is the cleanest condition you can give it.
Let us go back to the shopping cart. We will check whether the cart is empty and respond to the customer:
cart_items = []
cart_is_empty = len(cart_items) == 0
if cart_is_empty: print("Your cart is empty. Add something to continue.")else: print("You have items in your cart. Ready to pay!")Walk through it line by line:
cart_items = []makes an empty list, so there is nothing in the cart yet.len(cart_items)counts the items, which is0here, and== 0turns that into a boolean. Socart_is_emptybecomesTrue.if cart_is_empty:checks that boolean. Since it isTrue, the first message runs and theelsepart is skipped.
Because the cart starts empty, here is what the customer sees.
Output
Your cart is empty. Add something to continue.Now picture the same code but with cart_items = ["shoes", "socks"]. The list has things in it. So len(cart_items) is 2, the == 0 check becomes False, and Python runs the else line instead. Same code, different answer, all decided by one little boolean.
Because an empty list is already falsy, you can even skip the comparison and let the list speak for itself:
cart_items = []
if cart_items: print("You have items in your cart.")else: print("Your cart is empty.")Here if cart_items: quietly converts the list to a boolean. An empty list is falsy, so the else line runs.
Output
Your cart is empty.⚠️ Common Mistakes
A few slips trip up almost everyone at the start. Here are the ones to watch for.
- Writing
trueorfalsein lowercase. Python only understandsTrueandFalsewith a capital first letter. - Using one
=when you meant to compare.x = 5stores a value, whilex == 5asks a question. Only==gives you a boolean. - Wrapping booleans in quotes.
"True"is just text, not the booleanTrue. And text like"False"is actually truthy, because it has characters in it.
# ❌ Avoid: this is text, and non-empty text is truthyis_done = "False"
# ✅ Good: this is a real booleanis_done = FalseThe first version looks correct but behaves the opposite way you expect. The text "False" is truthy.
✅ Best Practices
These small habits keep your boolean code clear and easy to read.
- Name boolean variables as a yes-or-no question, like
is_active,has_paid, oris_empty. - Write
if is_ready:rather thanif is_ready == True:. The shorter form reads better and means the same thing. - Lean on truthy and falsy for empty checks.
if items:is cleaner thanif len(items) > 0:. - Keep each condition simple. If a check is getting long, store its result in a well-named boolean first, then use that.
🧩 What You’ve Learned
✅ A boolean is a value that is either True or False, always with a capital first letter.
✅ Comparisons like 5 > 3 and 10 == 10 produce booleans for you automatically.
✅ bool() converts any value into its True or False meaning.
✅ Falsy values are False, 0, "", [], and None; almost everything else is truthy.
✅ Booleans drive if statements, so they decide which path your program takes.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is the result of the expression 8 > 12 in Python?
Why: 8 is not greater than 12, so the comparison gives back the boolean False.
- 2
Which of these values is falsy in Python?
Why: An empty list is falsy, while non-empty text, non-zero numbers, and non-empty lists are all truthy.
- 3
Why does is_ready = true cause an error in Python?
Why: Python's boolean is written True with a capital T; lowercase true is treated as an unknown name and raises a NameError.
- 4
What does bool("") return?
Why: Empty text is one of Python's falsy values, so bool("") returns False.
🚀 What’s Next?
You now know how Python stores a yes-or-no answer. You also know how it decides truthy from falsy. Next we will look at how to change a value from one type to another on purpose, like turning text into a number.