Fundamentals 7 min read

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.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Conditional Statements: if, if‑else, if‑elif‑else, and Nested if

1. Single‑branch statement: if

Syntax: <code>if &lt;condition&gt;: 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 &lt;condition&gt;: 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 &lt;expr1&gt; if &lt;condition&gt; else &lt;expr2&gt; , where expr1 is evaluated when the condition is True, otherwise expr2 is evaluated.

3. Multi‑branch statement: if‑elif‑else

Syntax: <code>if &lt;condition1&gt;: block1 elif &lt;condition2&gt;: block2 ... elif &lt;conditionN&gt;: 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 &lt; a &lt; 10 over combining separate comparisons with logical operators for clarity.

Pythonprogramming fundamentalsConditional Statementsifcontrol flow
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.