Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Python operators turn values into decisions, and if, elif, and else send the program down the matching branch. The fictional check-in desk below shows how comparisons and Boolean logic work; its baggage limit and messages are invented teaching data, not airline policy.

How comparisons make a decision

Start with three fictional traveler values. A Boolean records whether the traveler has a ticket, a number records the bag count, and a string records the seat:

has_ticket = True
bags = 1
seat = "12A"
bag_limit = 2  # Invented teaching data

print(has_ticket == True)  # True: equality comparison
print(bags <= bag_limit)   # True: ordering comparison
print(seat == "12A")      # True: string values are equal

== asks whether two values are equal. Ordering comparisons include <, <=, >, and >=; they test how comparable values relate. These expressions produce truth values that can guide a conditional. For a Boolean variable, it is usually clearer to write if has_ticket: than if has_ticket == True:.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How if, elif, and else route execution

An if statement evaluates its condition and runs its indented block when that condition is true. Add zero or more elif branches for other conditions, then optionally add else as the fallback when none of the preceding conditions matched. Python’s tutorial notes that elif is short for “else if.”

if not has_ticket:
    message = "Check the booking"
elif bags > bag_limit:
    message = "Review the bag allowance"
else:
    message = "Continue to the next example"

Python checks the branches in order and runs only the first matching block. Here, the first condition handles a missing ticket; only if that is false does Python check the bag count. If both conditions are false, the else block runs. All outcomes and the limit are fictional examples, not actual check-in or boarding rules.

When to use equality, ordering, membership, or identity

Operators answer different questions. Use the operator that matches what you need to know:

Operator Question it answers Example
==, != Are these values equal, or different? seat == "12A"
<, <=, >, >= How do these ordered values compare? bags <= bag_limit
in, not in Is a value present in a container? seat in ["12A", "12B"]
is, is not Are these references to the very same object? result is None

For ordinary value checks, use ==, not is. Identity tests are useful for special singleton values such as None; they do not mean “equal in value.” Membership tests check for inclusion in a sequence or other container, rather than equality with the container itself.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How and and or combine conditions

Use and when both conditions must be true and or when either condition can be true. In this invented example, the traveler continues only if they have a ticket and the bag count is within the made-up limit:

if has_ticket and bags <= bag_limit:
    message = "Continue to the next example"
else:
    message = "Check the example conditions"

A fictional alternate route could accept either of two seat labels:

if seat == "12A" or seat == "12B":
    message = "Use the example seat group"

These are illustrative conditions, not live airline rules. Parentheses help make grouping explicit when a decision mixes and and or:

if has_ticket and (seat == "12A" or seat == "12B"):
    message = "Ticket and one of the example seats match"

Python evaluates comparisons before not, not before and, and and before or. Parentheses can clarify the intended grouping even when precedence already determines it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Short-circuiting and truth values

and and or evaluate operands from left to right and stop as soon as the result is determined. With and, a false left operand makes the whole expression false, so Python does not evaluate the right operand. With or, a true left operand makes the whole expression true, so the right operand is skipped.

This lets you check that a value exists before using it. For example, the second check runs only if seat is not None:

if seat is not None and seat.startswith("12"):
    message = "Seat starts with 12"

In a condition, False, None, numeric zero, and empty strings and containers are false. Other values are true unless an object defines its own truth value. Also, and and or can return one of their operands rather than a Boolean:

label = seat or "No seat recorded"

If seat is truthy, or returns it; otherwise it returns the fallback string. This is why a Boolean operator’s result should not always be assumed to be literally True or False.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Chained comparisons and a common syntax mistake

Python allows chained comparisons, which compare adjacent pairs and evaluate the middle expression once. For example, a hypothetical age range can be expressed as:

if 0 <= age < 18:
    message = "Age is in the example range"

This is equivalent in meaning to checking 0 <= age and age < 18 together, without evaluating age twice.

Do not confuse assignment with equality. = assigns a value, as in bags = 1; == compares values, as in bags == 1. Use the latter in a condition when you mean to test equality.

Practice the pattern with Python’s tutorial

Try changing the invented values and predicting which message appears before running the code. The official Python tutorial section on control flow introduces branching, while its section on conditions and data structures covers comparisons, membership, identity, and Boolean logic. The tutorial is freely available online and is aimed at programmers new to Python; see the Python tutorial overview.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.