0% complete

Control flow and Python collections · Lesson 1 of 3

Conditions and boolean logic

Branch on clear conditions and combine them without ambiguous truthiness.

Lesson

1. Concept and mental model

An if statement selects which block runs based on a condition. elif expresses another mutually exclusive condition and else handles the remaining case. Python uses indentation to mark the body of a block, so consistent indentation is part of the program’s syntax. Conditions can compare values with operators such as ==, !=, <, and >=, or test membership with in.

2. How it works

Boolean operators and, or, and not combine conditions. Parenthesize a complex expression or split it into well-named intermediate variables so that a reviewer can verify the logic. Python’s and and or short-circuit: the right-hand expression may not run when the left side already determines the result. This is useful for safe checks, but avoid hiding side effects in boolean expressions.

3. Use it safely and effectively

Many values have a truth value, so if items: checks whether a collection is nonempty. Prefer this idiom when truthiness is the actual rule; compare explicitly to zero or None when those distinctions matter. Avoid writing if flag == True; use if flag:. Test boundary conditions such as empty data, zero, negative values, and missing input, not only the happy path.

4. Syntax reference

score = 82
if score >= 90:
    grade = 'A'
elif score >= 70:
    grade = 'B'
else:
    grade = 'C'
print(grade)

5. Guided walkthrough

Evaluate the most specific condition first when cases overlap, and make each branch describe one meaningful outcome. Test boundary values on both sides of a threshold, since off-by-one mistakes often hide in conditions that appear correct for ordinary inputs.

6. Common mistakes to avoid

  • Writing assignment where a comparison is required, or using and when either condition should be enough.
  • Checking only typical values while missing exact boundaries such as zero, an empty string, or the threshold itself.

Visual guide

Decision flow

  1. Step 01 Evaluate

    Check the first condition.

  2. Step 02 Branch

    Run the first matching block.

  3. Step 03 Fallback

    Use else when no condition matches.

Code examples

Worked example
PYTHON
score = 82
if score >= 90:
    grade = 'A'
elif score >= 70:
    grade = 'B'
else:
    grade = 'C'
print(grade)
Expected output
B
Validate a score range
PYTHON
score = 84
if 0 <= score <= 100:
    print('valid')
else:
    print('invalid')
Expected output
valid
Use a default for an empty name
PYTHON
name = ''
label = name or 'Guest'
print(label)
Expected output
Guest

Key points

  • Branch on clear conditions and combine them without ambiguous truthiness.
  • Prefer explicit, testable behavior over hidden side effects.
  • Validate assumptions at system boundaries and handle failure deliberately.

Check your understanding

Practice

Which keyword handles a condition after a failed if condition?

Your learning progress

Finish this lesson