Technology Booleans and Comparisons | Python Tutorial

Learn with diagrams, code, systems and practical examples.

</>
TutorialsPythonBooleans and Comparisons
Core Python · Chapter 7

Booleans and Comparisons

Represent yes/no conditions and combine them safely.

Why this matters

Programs need to decide whether a user is allowed, a value is valid, or a task is complete.

Start with the idea

Boolean values are True and False. Comparisons create booleans, while and, or, and not combine or invert conditions.

IdeaUnderstand the purpose
CodeRun the smallest example
PracticeChange it yourself
SubjectVision learning pattern: understand the idea before memorizing syntax.

Small working example

Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.

Python
age = 21
has_ticket = True
can_enter = age >= 18 and has_ticket
print(can_enter)
Expected output
True
What to notice

The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.

Common beginner mistake

Mistake

Writing complex conditions without parentheses or intermediate variable names.

Better approach

Break large conditions into named booleans so each rule is understandable.

Quick recap

  • Comparisons return booleans.
  • Boolean operators combine rules.
  • Named conditions improve clarity.

Try it yourself

These are deliberately small. If you can complete them without copying the example, you are ready to continue.

  1. Write a condition for a valid discount.
  2. Use not to invert a condition.

Prepared deep chapter · Core Python

Booleans turn facts into decisions

A program often needs to answer a small yes-or-no question before it can decide what to do next. Is a password long enough? Is an order total above a discount threshold? Is a required field present? Did a search find the value we need? Python represents those answers with the Boolean values True and False.

The goal of this chapter is not to memorize every comparison symbol. It is to build a reliable decision habit: state the fact you want to test, write the smallest clear comparison, combine rules only when needed, then verify the result.

From facts to a Boolean decision A practical value such as order total is compared with a rule. The comparison produces True or False, and that Boolean result selects the next action. facttotal = 72 comparisontotal >= 50 Boolean resultTrue fact → comparison → True/False → next decision
Comparisons turn ordinary program state into a Boolean result you can use in a later if statement.

Start with the two Boolean values

Python's Boolean type is called bool. It has two values: True and False. Notice the capital letters. Writing true or false does not create Python Boolean values.

is_member = True
is_blocked = False

print(is_member)
print(is_blocked)
print(type(is_member).__name__)
Expected output
True
False
bool

You can write Boolean values directly, but most useful Booleans come from checking facts. A comparison such as age >= 18 asks a question and produces True or False.

Comparisons ask one precise question

Suppose an order gets free shipping at 50 dollars. The rule is easier to understand when the comparison looks like the sentence: “Is the order total at least 50?”

order_total = 72
has_free_shipping = order_total >= 50
print(has_free_shipping)
Expected output
True
Common comparisons
OperatorQuestionExample
==Are the values equal?status == "paid"
!=Are the values different?role != "guest"
<Is the left value smaller?temperature < 0
<=Is the left value smaller or equal?attempts <= 3
>Is the left value larger?stock > 0
>=Is the left value larger or equal?age >= 18
Common mistake

= assigns a value. == compares two values. If you are asking “are these equal?”, use ==.

Chained comparisons can express a range clearly

Python lets you write a range check in the same order you might say it:

temperature = 21
comfortable = 18 <= temperature <= 24
print(comfortable)
Expected output
True

The middle value is evaluated once. This is usually clearer than writing the same value twice in a longer and expression.

Truthiness lets values participate in yes/no checks

Python can treat many values as true or false in a condition. Empty strings, empty lists, empty dictionaries, zero, None, and False are common falsey values. Many non-empty values are truthy.

coupon_code = ""
items = ["book", "pen"]

print(bool(coupon_code))
print(bool(items))
Expected output
False
True
Beginner rule

Use truthiness when the meaning really is “empty versus not empty.” If your domain distinguishes zero, missing, blank, and invalid values, write the more precise check instead of hiding those states behind one truthy/falsy test.

Combine rules with and, or, and not

Once each individual fact is clear, Boolean operators let you combine them.

and

Both sides must be truthy.

can_checkout = has_items and payment_ready
or

At least one side must be truthy.

can_edit = is_owner or is_admin
not

Invert a truth value.

should_retry = not request_succeeded

When a condition becomes long, name the individual rules first. Good names make the decision explain itself.

age = 21
has_ticket = True
is_banned = False

is_adult = age >= 18
can_enter = is_adult and has_ticket and not is_banned
print(can_enter)
Expected output
True

Short-circuiting can prevent unnecessary or unsafe work

Python evaluates and and or from left to right and can stop as soon as the result is determined. This is called short-circuiting.

user = None
can_read_name = user is not None and user.name != ""
print(can_read_name)
Expected output
False

Because user is not None is false, Python does not evaluate the second part. That prevents an attribute lookup on None. Short-circuiting is useful, but it should not become a trick for hiding confusing side effects inside conditions.

is, ==, and in answer different questions

These operators can look similar because they all produce Boolean-like results, but they ask different questions.

Value equality: ==

Use it when you care whether two values compare equal.

status == "paid"
Object identity: is

Use it when you care whether two references point to the very same object. The common beginner use is value is None.

result is None
Membership: in

Use it to ask whether a value belongs to a container.

role in {"admin", "editor"}
Common mistake

Do not use is as a replacement for == when comparing normal strings or numbers. Identity and equality are different questions.

Build a checkout eligibility rule step by step

A checkout can continue only when the cart has items, the account is active, the delivery country is supported, and the order is below a manual-review threshold.

items = ["book", "pen"]
account_active = True
country = "US"
order_total = 86
supported_countries = {"US", "CA", "GB"}

has_items = bool(items)
country_supported = country in supported_countries
within_limit = order_total < 500

can_checkout = (
    has_items
    and account_active
    and country_supported
    and within_limit
)

print(can_checkout)
Expected output
True

Each named Boolean answers one question. That makes the final rule readable and gives you useful checkpoints when debugging.

Five predictable beginner mistakes

Confusing = and ==

Assignment changes a binding; equality asks a question.

Using is for value equality

Use == for ordinary value comparison; reserve identity tests for identity questions such as is None.

Hiding too many states behind truthiness

0, "", and None can all be falsey, but they may mean different things in your domain.

Writing one giant condition

Break important rules into named Booleans so readers can inspect them independently.

Forgetting short-circuit order

Put a guard such as value is not None before an access that depends on it.

Practice before moving on

  1. Create named Boolean rules for a discount that requires a member account and a total of at least 40.
  2. Predict the result of 0 <= temperature < 10 for temperature = -2, 0, and 7.
  3. Write a condition that checks whether a role is in {"admin", "editor"} and the account is not suspended.
  4. Compare value is None with value == None. Use the Python style you would choose in real code and explain why.
  5. Refactor one long and/or condition into three named Boolean variables.

Quick recap

  • True and False are the two Boolean values.
  • Comparisons turn facts into Boolean results.
  • Truthiness is useful for empty/non-empty checks but can hide meaningful domain states.
  • and, or, and not combine or invert rules, and and/or short-circuit.
  • == checks value equality, is checks identity, and in checks membership.

Next chapter: If, Elif, and Else — where Boolean results decide which code actually runs.

Official references used in this chapter

Prediction lab · prepared for the next Python chapter

Trace the decision before you trust the result

Boolean code becomes easier when you stop reading it as one long sentence and instead inspect one fact at a time. For each trace below, predict the result first, then read the explanation. The goal is not speed. The goal is to build a repeatable debugging habit you can use when a condition looks correct but behaves differently from what you expected.

Trace 1 · Empty input is not the same as missing input
coupon = ""
customer_id = None

has_coupon = bool(coupon)
has_customer = customer_id is not None

print(has_coupon)
print(has_customer)

Expected output: False and then False.

The first check asks whether the string is empty. The second asks whether a value exists at all. Both happen to be false here, but they describe different states. If your program needs to distinguish “blank text” from “missing value,” one truthiness check is too vague.

Trace 2 · Short-circuiting protects a dependent check
profile = None

can_read_name = (
    profile is not None
    and profile.name != ""
)

print(can_read_name)

Expected output: False.

Python evaluates the left side of and first. Once profile is not None is false, the complete expression cannot become true, so Python does not evaluate profile.name. This is useful because the second check only makes sense when a profile object exists.

Common mistake: reversing those two checks would try to read profile.name before proving that profile is an object.

Trace 3 · Membership and equality answer different questions
role = "editor"
allowed_roles = {"admin", "editor"}

is_editor = role == "editor"
can_manage = role in allowed_roles

print(is_editor)
print(can_manage)

Expected output: True and then True.

The equality check asks whether the role is exactly one value. The membership check asks whether that value belongs to a set of approved choices. When requirements grow from one accepted value to several, membership often describes the business rule more clearly.

Deeper detail · Python-specific behavior

and and or return operands, not always True or False

Python first tests truthiness, but the value returned by and or or can be one of the original operands. This is useful for compact fallback patterns, but it can surprise beginners who expect every Boolean-looking expression to produce the bool type.

or returns the first truthy operand
display_name = "" or "Guest"
print(display_name)
print(type(display_name).__name__)

Expected output: Guest, then str.

The empty string is falsey, so Python evaluates and returns the second operand. The result is a string, not the Boolean value True.

and returns the first falsey operand, otherwise the last operand
profile = {"name": "Asha"}
name = profile and profile["name"]
print(name)

profile = {}
name = profile and profile["name"]
print(name)

Expected output: Asha, then {}.

With a non-empty dictionary, Python evaluates the second operand and returns the name. With an empty dictionary, it stops immediately and returns that empty dictionary.

Beginner rule

If your intent is “I need an actual Boolean,” use a comparison or wrap the expression in bool(...). Use operand-returning and/or patterns only when returning a real value is intentional and clear to the next reader.

Quick reference · bookmark-worthy decision aid

Choose the question before you choose the operator

When a Boolean condition gets confusing, start with the sentence you are trying to answer. The table below maps common intentions to a clear Python pattern and the mistake most likely to blur the meaning.

Intent-first Boolean reference
Question you meanClear Python patternWatch for
Is the value missing?value is NoneDo not replace this with not value if 0 or "" are valid values.
Is a collection empty?not itemsUse this only when empty/non-empty is really the business rule.
Does this value equal one expected value?status == "paid"is asks about object identity, not normal value equality.
Is this value one of several allowed choices?role in allowed_rolesA long chain of or comparisons is harder to extend and review.
Is a number inside a range?18 <= age <= 64Check whether the endpoints should be included before choosing < or <=.
Can I safely run a dependent check?profile is not None and profile.name != ""Put the guard first so short-circuiting can protect the dependent access.

Use this as a debugging worksheet: say the question in plain language, pick the matching pattern, then print or inspect the smaller facts before combining them.

Try three small changes

  1. Change coupon to "SAVE10" and predict which trace value changes. Then change customer_id to 0 and explain why customer_id is not None becomes true even though bool(0) would be false.
  2. Add "viewer" to allowed_roles, set role = "viewer", and predict why is_editor becomes false while can_manage can remain true.
  3. Change display_name = "" or "Guest" to display_name = "Mira" or "Guest". Predict both the value and its type, then explain why or did not return True.
Debugging habit

When a Boolean expression surprises you, write down the value of each named fact before combining them. A clear sequence of small checks is easier to verify than a clever one-line condition.