python if elseif
时间: 2023-10-17 15:15:15 浏览: 150
python if 语句
In Python, the `if-elif-else` statement is used to check multiple conditions and execute different blocks of code based on the condition that is true. Here is an example:
```
x = 10
if x < 5:
print("x is less than 5")
elif x > 5 and x < 15:
print("x is between 5 and 15")
else:
print("x is greater than or equal to 15")
```
In this example, the variable `x` is first checked if it is less than 5. If it is not, then it is checked if it is between 5 and 15. If it is not, then the `else` block is executed.
Note that the `elif` statement is shorthand for "else if". You can have multiple `elif` statements in an `if` block to check for additional conditions.
阅读全文