> For the complete documentation index, see [llms.txt](https://beret81.gitbook.io/study-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://beret81.gitbook.io/study-notes/python/tiao-jian-fen-zhi-yu-xun-huan/if-tiao-jian-fen-zhi.md).

# if 条件分支

> if 语句是python 语音基本的条件分支判断语句，它为代码的逻辑判断提供了操作方法。

### 1、if 语句的基本用法

### 单分支判断

```
语法：
if 条件：
    满足条件执行代码
    
例如：
if 9>1:
    print('ok')    #条件为真则打印输出，否则无输出
```

### 双分支判断

```
语法：
if 条件:
    满足条件执行代码
else:
   if条件不满足就走这段
   
例如：
if 5>4:
    print('正确')
else:
    print('错误')
```

### 多分支判断

```
语法：
if 条件:
    满足条件执行代码
elif 条件:
    上面的条件不满足就走这个
elif 条件:
    上面的条件不满足就走这个
elif 条件:
    上面的条件不满足就走这个    
else:
    上面所有的条件不满足就走这段
    
例如：

score = int(input("输入分数："))

if score > 100:
    print("最高分为100")
elif score >= 90:
    print("优秀")
elif score >= 80:
    print("良好")
elif score >= 60:
    print("及格")
elif score >= 40:
    print("不及格")
else:
    print("太笨了！")
```

### 2、if 语句练习题
