if then python
时间: 2023-07-09 08:32:58 浏览: 40
If-then statements are a fundamental part of programming in Python. They allow you to execute code based on certain conditions being met. Here's an example of an if-then statement in Python:
```
x = 10
if x > 5:
print("x is greater than 5")
```
In this example, we're setting the value of `x` to `10`, and then using an if-then statement to check if `x` is greater than `5`. If the condition is true, the code within the `if` block is executed, which prints out the message "x is greater than 5".
You can also include an `else` block to execute code if the condition in the `if` statement is false:
```
x = 2
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
```
In this example, since `x` is less than `5`, the code within the `else` block is executed, which prints out the message "x is less than or equal to 5".
阅读全文