Python operators turn values into comparisons and logical tests; if, elif, and else use those tests to choose which indented code runs. The examples below use a fictional check-in desk to show how the pieces fit together—not to describe real airline rules.
Start with values and comparisons
Imagine a small program tracking a fictional traveler. The variables below are invented teaching data; bag_limit is not an actual baggage allowance.
As an Amazon Associate I earn from qualifying purchases.
has_ticket = True
bags = 1
bag_limit = 2
seat = "12A"
Comparison operators produce a truth value that can guide a decision:
| Operator | Example | What it checks |
|---|---|---|
== |
seat == "12A" |
Whether the values are equal. |
< |
bags < bag_limit |
Whether the left value is less than the right. |
>= |
bags >= 1 |
Whether the left value is greater than or equal to the right. |
Use == to compare values in ordinary checks. A single = is assignment, which binds a value to a variable; it is not the equality test to use in an if condition.
#1 Best Overall
Route execution with if, elif, and else
An if statement evaluates its condition and runs the indented block when that condition is true. You can add zero or more elif branches (“else if,” as the Python tutorial explains) and an optional else fallback. Python checks the branches in order and runs the first matching block.
if not has_ticket:
message = "Check the booking"
elif bags > bag_limit:
message = "Review the bag allowance"
else:
message = "Continue to the next example"
All three outcomes and the bag limit are invented for the lesson. The else branch means only that neither earlier condition matched; it does not represent an airline decision. Each block must be indented consistently. See the Python tutorial’s control-flow section.
Combine conditions with and and or
Use and when both conditions must be true, and or when either condition is sufficient. For example, a made-up program might require both a ticket and bags within its invented limit:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Rank #2
can_continue = has_ticket and bags <= bag_limit
Here, the left side checks the ticket flag; the right side compares the bag count with the fictional limit. A hypothetical alternate route could accept either a ticket or a staff override:
has_override = False
can_enter_example = has_ticket or has_override
These are programming examples, not check-in criteria. Parentheses help show intended grouping when combining operators:
ready_for_example = has_ticket and (seat == "12A" or seat == "12B")
This means the ticket must be present and the seat must match either of the two example strings. Without parentheses, precedence still determines how an expression is grouped, but explicit grouping can make the logic easier to read.
Understand truth values and short-circuiting
In a condition, False, None, numeric zero, and empty strings or containers are false. Other values are true unless an object defines its own truth value. This means a condition can test a value directly, not just the result of a comparison:
seat = "12A"
if seat:
message = "A seat value was supplied"
and and or evaluate operands from left to right and short-circuit when the result is already determined. They can return one of their operands—not necessarily the Boolean values True or False. For instance, has_ticket and seat returns the first false operand if there is one; otherwise it returns seat.
Short-circuiting is useful when the second test depends on the first being satisfied:
if seat and seat.startswith("12"):
message = "Seat is in the example row"
If seat is empty, the left side is false and Python does not call startswith. The second operation is therefore protected from being called on an empty value.
Choose membership, identity, and ordering checks for their purpose
Not every comparison asks whether two values are equal. Python also provides membership tests and identity tests; they answer different questions.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchinandnot incheck whether a value is present in a container. For example,seat in {"12A", "12B"}asks whether the seat string is one of those set members.isandis notcheck whether two references point to the same object. A common appropriate use isvalue is None; do not substituteisfor value equality such asseat == "12A".- Ordering operators such as
<and>=compare orderable values, such as numbers. They are not a replacement for equality checks.
For a range, Python supports chained comparisons. 0 <= age < 18 means 0 <= age and age < 18; the middle expression is evaluated once.
Best Value
Read precedence without guessing
Python groups these operators from tightest to loosest: comparisons first, then not, then and, then or. Thus a < b and c groups as (a < b) and c. Add parentheses when they clarify the intended decision, especially when mixing and and or. The Python language reference documents truth-value testing, Boolean evaluation, comparisons, and precedence; the tutorial also covers conditions and comparison types.
Practice with the official tutorial
The official Python tutorial is freely available online and is aimed at programmers new to Python. Try changing one value at a time in the examples and predicting which branch runs before executing the code.
Quick Recap
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.
Free tools Windows power users keep installed
One-click scans. No signup required.




