Python if Statement
In Python programming,if statement is the most basic and most important conditional control structure. It decides whether to execute a particular block of code based on whether a condition is true or false.
Python's if The statement is very intuitive and readable; it defines code blocks through indentation, which is a major feature distinguishing Python from other languages.
Word meaning: if means "if" and is the entry point of conditional judgment.
Basic syntax and parameters
if The statement is a standalone code block and must be used with a conditional expression.
Syntax format
if conditional expression:
代码块
Syntax Description
- Conditional expression: Any expression that returns a Boolean value (non-zero numbers, non-empty strings and non-empty containers are considered true).
- Colon: A colon must follow the conditional expression.
- Indentation: The code block must be indented (usually 4 spaces); indented code belongs to the sub-block of the if statement.
Return value / effect
- no return value: The if statement is a control flow statement and returns no value.
- effect: When the condition is True the indented block is executed, otherwise it is skipped.
Example
Let us thoroughly master if the usage of the statement.
Example 1: basic usage - comparing numbers
Example
Basic if statement
age
=
18
if
age
=
18
:
(
"Adult"
)
(
"Program ends"
)
Expected output:
成年人
程序结束
Code analysis:
age >= 18is a conditional expression that returns True.- Because the condition holds, execute
print("成年人")。 print("程序结束")is not inside the indented block of the if, so it is executed regardless of the condition.
Example 2: checking whether a string is empty
Example
Check the string
name
=
""
An empty string is False
if
name:
(
f
"Hello, {name}"
)
else
:
(
"Please enter a name"
)
Non-empty string
name
=
"Tom"
if
name:
(
f
"Hello, {name}"
)
Expected output:
请输入名字
你好, Tom
Code analysis:
- In Python, empty strings, empty lists and empty dictionaries are considered False in conditions.
- A non-empty string is considered True.
Example 3: multiple conditions
Example
Compound condition
score
=
85
Use logical operators
if
score
=
60
and
score
<
90
:
(
"Passed"
)
Check multiple conditions (simplified form)
if
60
<=
score
<
90
:
Chained comparisons, a Python speciality
(
"Score is between 60 and 90"
)
Use in for testing
colors
=
[
"red"
,
"green"
,
"blue"
]
if
"red"
in
colors:
(
"Contains red"
)
Expected output:
及格了
成绩在 60-90 之间
包含红色
Code analysis:
- Python supports chained comparisons, such as
60 <= score < 90。 inoperator is used to test whether an element is in a sequence.
Example 4: single-line if expression (ternary operator)
Example
Python's ternary expression
age
=
20
Format: value1 if condition else value2
result
=
"Adult"
if
age
=
18
else
"Minor"
(
result
)
Output: Adult
A more complex example
x
=
10
y
=
20
max_val
=
x
if
x
y
else
y
(
f
"Larger value: {max_val}"
)
Output: Larger value: 20
Expected output:
成年
较大值: 20
Python's ternary expression is more concise than in traditional languages; its format is value1 if condition else value2。
Python3 Conditional Control