Python Implement if-else Statement
In Python,if-else statement is used to execute different code blocks based on a condition. If the condition is True, the code in the if block is executed; if the condition is False, the code in the else block is executed.
Example
Define a variable
x
=
10
Use an if-else statement
if
x
5
:
(
"x is greater than 5"
)
else
:
(
"x is less than or equal to 5"
)
Code analysis:
x = 10: define a variablexand assign it the value 10.if x > 5:: Check whetherxis greater than 5. If the condition is true, execute theifblock is executed.print("x 大于 5"): Ifxis greater than 5, output "x is greater than 5".else:: Ififcondition is false, execute theelseblock is executed.print("x 小于或等于 5"): Ifxis not greater than 5, output "x is less than or equal to 5".
Output result:
x 大于 5
Python3 Examples