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 makes decisions by evaluating a condition as true or false, then running the matching indented block. Comparison operators build many conditions; if, elif and else control which block runs. You can also combine conditions with Boolean operators or use a condition directly when a value is expected.

Start with a comparison and an if block

A comparison such as temperature > 25 produces a Boolean result: True or False. An if statement runs its indented body when that result is true:

temperature = 28
if temperature > 25:
    print("It is warm")

The colon marks the start of the block, and indentation identifies the statements inside it. If the condition is false, Python skips the block. Indentation is part of Python’s syntax, not just visual formatting.

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

Choose between cases with else and elif

Add else when you want a fallback for every case not handled by the preceding if. Use elif for one or more ordered alternatives. Python checks the conditions from top to bottom and runs only the first matching branch.

score = 82
if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
else:
    grade = "Keep practicing"

Here, a score of 82 does not meet the first condition, so Python checks the elif condition and assigns "B". Conditions can overlap: putting score >= 80 first would catch scores that might otherwise qualify for an A. Order branches from the most specific or highest-priority case to the next. There can be zero or more elif clauses; elif is short for “else if.”

Pick the operator that matches the question

These operators answer different kinds of questions. Use value comparisons for equality and ordering, membership to check whether an item belongs to a collection, and identity only when whether two references point to the same object matters.

Question Operator Example
Do these values compare equal or unequal? ==, != answer == "yes"
Is one value ordered before or after another? <, <=, >, >= age >= 18
Is an item present in a collection? in, not in answer in ("y", "yes")
Are these references to the same object? is, is not value is None

== compares values; is tests object identity. Two distinct objects may hold equal values, so use == for ordinary value comparison. A common identity check is value is None.

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

Do not confuse assignment with comparison: = assigns a value, while == asks whether values are equal. For example, score = 82 stores a value; score == 82 evaluates to a Boolean.

Use chained comparisons for ranges

Python lets you express a range test in one readable comparison: 0 <= score < 100. It means the score is at least 0 and less than 100. Python evaluates the middle expression once, so this is useful when checking that a value falls between bounds.

Chaining also works beyond ranges: a < b > c means a < b and b > c; it does not compare a directly with c. For beginner code, use familiar range chains where their meaning is immediately clear.

Combine conditions with and, or and not

Use and when both conditions must hold, or when either can hold, and not to negate a condition:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
if (age >= 18) and has_id:
    print("Entry allowed")

Parentheses make the intended grouping obvious. Python’s precedence order is not, then and, then or; arithmetic binds more tightly than comparisons, which bind more tightly than these Boolean operators. For example, not a == b means not (a == b). Parentheses are especially helpful when a condition mixes and and or.

These operators short-circuit. and stops as soon as it encounters a false operand; or stops at the first true operand. Any later operand is not evaluated. This can make a guarded condition safe, but avoid hiding side effects in conditions because short-circuiting may prevent them from running.

Unlike comparisons and not, and and or do not necessarily return True or False. They return one of their operands: x and y returns x if x is false, otherwise y; x or y returns x if x is true, otherwise y. In conditions, focus first on what their truth values mean.

Understand truthiness

A condition does not have to be a comparison. Python tests the truth value of the expression itself. False, None, numeric zero, and empty strings and containers are false; other values are true unless an object’s __bool__() method customizes its behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
names = []
if names:
    print("There are names")
else:
    print("The list is empty")

An empty list is false in a condition, while a non-empty list is true. Truthiness is convenient when the condition is simply whether a collection has content. Use an explicit comparison when the exact value matters, such as count == 0.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use a conditional expression for a simple value choice

When you need to choose one of two values rather than run blocks of actions, Python’s conditional expression can keep the choice compact:

label = "adult" if age >= 18 else "minor"

Its form is value_if_true if condition else value_if_false. It evaluates the condition and produces one value. Prefer statement-based if/elif/else when each case needs multiple steps or when a compact expression would be hard to read.

When to use match instead

For some selections based on patterns, a constant-like value, or structural shape, Python’s match statement may express the alternatives more naturally. It is not a universal replacement for if: use if for general Boolean conditions and ordered ranges, and consider match when the decision is fundamentally about which pattern a value fits.

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

Check the official Python documentation

The Python 3.14.7 tutorial and language reference describe the syntax and behavior covered here. The semantics are longstanding, but consult the documentation for the Python version you use if you need version-specific details.

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.