Conditional logic selects which code runs by testing conditions. Python provides if, optional elif clauses, and an optional else fallback; indentation defines each branch. section 3.4.2 section 8.1
Use the construct according to these rules:
- The if body is required and runs when its condition evaluates to True. section 8.1
- Python checks elif conditions only after earlier conditions are false, then executes the first matching branch. section 8.1
- The else body runs only when no tested condition is true; both elif and else can be omitted. section 3.4.2
- Indent statements consistently inside each branch; use pass when a required branch should perform no action. section 3.4.2 section 8.1
Classifying a guess
guess = 4 target = 7 if guess < target: print("Low") elif guess == target: print("Correct!") else: print("High") # Low section 8.1
x = 5
if x < 5:
y = -1
z = 5
elif x > 5: #1
y = 1
z = 11
else: #1
y = 0 #2
z = 10 #2
print(x, y, z)
See a compact example where equal, lower, and higher values assign different results before printing them. section 3.4.2




















