Python Branching Statements: if, if‑else, if‑elif‑else, and Nested Structures
This article explains Python's branching statements—including single‑branch if, two‑branch if‑else, multi‑branch if‑elif‑else, and nested structures—provides syntax, example code for number comparison, ID gender detection, BMI calculation, and discusses best practices in Python programming.
Python branching statements, also known as selection statements, represent the program's decision structure, allowing different actions based on conditions.
Branching statements are divided into single‑branch, two‑branch, and multi‑branch structures; only one path can be chosen, so conditions must be designed carefully.
1. Single‑branch statement: if statement
The syntax is:
<code>if 条件表达式:
语句块</code>Example: compare two numbers and output the smaller one.
<code>num_a = int(input('please input a number:'))
num_b = int(input('please input another number:'))
if num_a > num_b:
num_a, num_b = num_b, num_a # swap two numbers
print("the smaller one is", num_a)</code>When the condition is true, the block executes; otherwise it is skipped.
2. Two‑branch statement: if else statement
The structure adds an else clause that runs when the if condition is false.
<code>if 条件表达式:
语句块1
else:
语句块2</code>Example: output the smaller number using if else .
<code>num_a = int(input('please input a number:'))
num_b = int(input('please input another number:'))
if num_a > num_b:
print("the smaller one is", num_b)
else:
print("the smaller one is", num_a)</code>Another example: determine gender from an ID number.
<code>id_code = input('请输入身份证号码:')
number = int(id_code[-2])
if number % 2 == 0:
print("女性")
else:
print("男性")</code>The else clause implicitly tests number%2 == 0 being false, i.e., the number is odd.
3. Multi‑branch structure: if – elif – else statement
This extends the two‑branch form to multiple exclusive branches.
<code>if 条件表达式1:
语句块1
elif 条件表达式2:
语句块2
...
elif 条件表达式n:
语句块n
else:
语句块n+1</code>Example: calculate BMI and classify the result.
Category
Range
Underweight
Below 18.5
Normal
18.5‑25 (exclusive)
Overweight
25‑30 (exclusive)
Obese
30‑35 (exclusive)
Severe Obesity
35 and above
<code>weight = float(input("请输入你的体重(Kg):"))
height = float(input("请输入你的身高(m):"))
BMI = weight / height ** 2
if BMI < 0:
print("输入错误")
elif BMI < 18.5:
print("偏瘦")
elif BMI < 25:
print("正常")
elif BMI < 30:
print("偏胖")
elif BMI < 35:
print("肥胖")
else:
print("重度肥胖")</code>The conditions are mutually exclusive, ensuring only one output is produced.
4. Nested branching
Nested if statements allow further condition checks inside an outer branch.
<code>if 条件表达式1:
…
if 条件表达式2:
语句块1
else:
语句块2
else:
语句块3</code>Example: validate an ID number's length before determining gender.
<code>id_code = input('请输入身份证号码:')
if len(id_code) == 18:
number = int(id_code[-2])
if number % 2 == 0:
print("女性")
else:
print("男性")
else:
print("输入不合法")</code>After the tutorial, promotional material invites readers to scan QR codes for free Python learning resources.
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.