if语句和if-else语句的格式
时间: 2023-11-10 19:07:37 浏览: 116
if语句和if-else语句的格式如下:
```python
# if语句
if 条件表达式:
代码块
# if-else语句
if 条件表达式:
代码块1
else:
代码块2
```
其中,条件表达式的值为True或False,如果为True,则执行代码块1,否则执行代码块2。
举个例子,如果要判断一个数是否为正数,可以使用if语句:
```python
num = 10
if num > 0:
print("这个数是正数")
```
如果要在不满足条件时显示另外一个语句,可以使用if-else语句:
```python
num = -5
if num > 0:
print("这个数是正数")
else:
print("这个数不是正数")
```
阅读全文
相关推荐
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)