python if else
时间: 2023-10-17 12:15:24 浏览: 47
In Python, the `if else` statement is used for conditional execution of code. It is used when we want to execute a specific block of code only when a certain condition is met. The syntax for `if else` statement is as follows:
```
if condition:
# Code to execute when condition is true
else:
# Code to execute when condition is false
```
Here, `condition` is any expression that can be evaluated to either `True` or `False`. If the `condition` is `True`, then the code inside the `if` block is executed. Otherwise, the code inside the `else` block is executed.
For example, let's say we want to print a message depending on whether a number is even or odd. We can use the `if else` statement as follows:
```
num = 10
if num % 2 == 0:
print("The number is even")
else:
print("The number is odd")
```
Here, we are checking if the remainder of `num` divided by 2 is equal to 0. If it is, then we print "The number is even". Otherwise, we print "The number is odd".