Free tools Windows power users keep installed
One-click scans. No signup required.
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:.
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 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteHow 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.”
#1 Best Overall
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.
Rank #2
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.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →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.
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:
Best Value
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.
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.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errors

