# 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 语句练习题


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://beret81.gitbook.io/study-notes/python/tiao-jian-fen-zhi-yu-xun-huan/if-tiao-jian-fen-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
