Python Conditional Statements: if, if‑else, if‑elif‑else, and Nested if
This article explains Python's conditional statements, covering the syntax and usage of single‑branch if, two‑branch if‑else, multi‑branch if‑elif‑else structures, the pass keyword, ternary expressions, nesting rules, and best‑practice tips for writing clear and efficient control‑flow code.
1. Single‑branch statement: if
Syntax: <code>if <condition>: statement block</code>
Notes: the condition is evaluated; if True the block runs, otherwise it is skipped. The block is defined by indentation. A short form can be used, e.g., if tag: print("True") . Objects like 0, 0.0, None, empty strings, lists, etc., evaluate to False; everything else evaluates to True. Use the condition directly instead of if tag == True/False .
The pass keyword creates an empty statement that does nothing, useful as a placeholder to keep the program structure valid.
2. Two‑branch statement: if‑else
Syntax: <code>if <condition>: statement block1 else: statement block2 # executes when condition is False</code>
Notes: a colon must follow the condition, both blocks must be indented, and else aligns with if . The else clause is optional.
A compact form, the conditional expression, can be written as <expr1> if <condition> else <expr2> , where expr1 is evaluated when the condition is True, otherwise expr2 is evaluated.
3. Multi‑branch statement: if‑elif‑else
Syntax: <code>if <condition1>: block1 elif <condition2>: block2 ... elif <conditionN>: blockN else: blockN+1</code>
Notes: elif is short for else if and can appear multiple times. Python evaluates conditions sequentially and executes the first block whose condition is True, then exits the whole structure. If none match, the optional else block runs. Place simpler or more frequently true conditions earlier for readability and efficiency.
4. Nested if statements
Nested forms include:
if inside another if
elif containing an if
else containing an if
Notes: there is no syntactic limit on nesting depth, but for maintainability it is advisable not to exceed three levels; consider merging conditions or extracting logic into functions. Pay careful attention to matching indentation to avoid errors.
5. Other tips
Avoid comparing values of different types with == or != because the result may be unintuitive.
When a loop (e.g., for ) is nested inside a conditional, ensure proper indentation so the else aligns with the correct block.
Prefer chained comparisons like 0 < a < 10 over combining separate comparisons with logical operators for clarity.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.