Python/python5/If.md

125 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 一、条件语句 #
Python 条件语句跟其他语言基本一致的,都是通过一条或多条语句的执行结果( True 或者 False )来决定执行的代码块。
Python 程序语言指定任何非 0 和非空null值为 True0 或者 null为 False。
执行的流程图如下:
![if语句流程图](https://user-gold-cdn.xitu.io/2017/6/23/9b796437c2f0ac3ba1dc5009de091031)
## 1、if 语句的基本形式 ##
Python 中if 语句的基本形式如下:
```python
if 判断条件
执行语句……
else
执行语句……
```
前面也提到过Python 语言有着严格的缩进要求,因此这里也需要注意缩进,也不要少写了冒号 `:`
if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
例如:
```python
# -*-coding:utf-8-*-
results=59
if results>=60:
print ('及格')
else :
print ('不及格')
```
输出的结果为:
```txt
不及格
```
上面也说道,非零数值、非空字符串、非空 list 等判断为True否则为False。因此也可以这样写
```python
num = 6
if num :
print('Hello Python')
```
## 2、if 语句多个判断条件的形式 ##
有些时候,我们的判断语句不可能只有两个,有些时候需要多个,比如上面的例子中大于 60 的为及格,那我们还要判断大于 90 的为优秀,在 80 到 90 之间的良好呢?
这时候需要用到 if 语句多个判断条件,
用伪代码来表示:
```python
if 判断条件1:
执行语句1……
elif 判断条件2:
执行语句2……
elif 判断条件3:
执行语句3……
else:
执行语句4……
```
实例:
```python
# -*-coding:utf-8-*-
results = 89
if results > 90:
print('优秀')
elif results > 80:
print('良好')
elif results > 60:
print ('及格')
else :
print ('不及格')
```
输出的结果:
```txt
良好
```
## 3、if 语句多个条件同时判断 ##
Python 不像 Java 有 switch 语句,所以多个条件判断,只能用 elif 来实现,但是有时候需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。
```python
# -*-coding:utf-8-*-
java = 86
python = 68
if java > 80 and python > 80:
print('优秀')
else :
print('不优秀')
if ( java >= 80 and java < 90 ) or ( python >= 80 and python < 90):
print('良好')
```
输出结果:
```txt
不优秀
良好
```
注意if 有多个条件时可使用括号来区分判断的先后顺序,括号中的判断优先执行,此外 and 和 or 的优先级低于 >(大于)、<(小于)等判断符号,即大于和小于在没有括号的情况下会比与或要优先判断。