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:
if <condition>:
statement blockNotes: 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:
if <condition>:
statement block1
else:
statement block2 # executes when condition is FalseNotes: 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:
if <condition1>:
block1
elif <condition2>:
block2
...
elif <conditionN>:
blockN
else:
blockN+1Notes: 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
