tongsiying

阅读|运动|自律

0%

第3篇:条件语句

本节目标

掌握Python条件语句的知识点及应用

一、条件语句的定义

Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。

可以通过下图来简单了解条件语句的执行过程:

代码执行过程

条件语句的定义为:

1
2
3
4
5
6
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
  • 如果 “condition_1” 为 True 将执行 “statement_block_1” 块语句
  • 如果 “condition_1” 为False,将判断 “condition_2”
  • 如果”condition_2” 为 True 将执行 “statement_block_2” 块语句
  • 如果 “condition_2” 为False,将执行”statement_block_3”块语句

Python 中用 elif 代替了 else if,所以if语句的关键字为:if – elif – else

注意:

  • 1、每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块。
  • 2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
  • 3、在Python中没有switch – case语句。

提醒:统一缩进问题(都是使用四个空格 = tab)

二、基本条件语句

1
2
3
4
5
6
if 条件表达式:
条件表达式的值为真,执行此缩进中的代码
...
else:
条件表达式的值为假,执行此缩进中的代码
...

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
""" 1. 单独的if """
age_num = 23
# if 如果年龄大于18
if age_num > 18:
print("已成年,可以去网吧嗨皮……")

""" 2. if else """
age_num = 23

if age_num > 18:
print("已成年,可以去网吧嗨皮……")
else:
print("未成年,回家写作业吧……")

三、多条件判断

1
2
3
4
5
6
7
8
9
10
11
if 条件A:
A成立,执行此缩进中的代码
...
elif 条件B:
B成立,执行此缩进中的代码
...
elif 条件C:
C成立,执行此缩进中的代码
...
else:
上述都不成立
  • 示例:输入一个数字,判断星期几

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    """ 例2. if elif ... else """
    day = int(input('请输入一个数字(1-7):'))
    if day == 1:
    print('星期一')
    elif day == 2:
    print('星期二')
    elif day == 3:
    print('星期三')
    elif day == 4:
    print('星期四')
    elif day == 5:
    print('星期五')
    elif day == 6:
    print('星期六')
    elif day == 7:
    print('星期日')
    else:
    print('数字不能超过7')
  • 示例2

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    """ 例2 成绩分类"""
    score = input('请输入分数:')
    data = int(score)

    if data > 90:
    print('优')
    elif data > 80:
    print('良')
    elif data > 80:
    print('中')
    elif data > 80:
    print('差')
    else:
    print('不及格')

四、条件嵌套

在嵌套 if 语句中,可以把 if...elif...else 结构放在另外一个 if...elif...else 结构中。

1
2
3
4
5
6
7
8
9
10
11
12
if 条件A:
if 条件A1:
...
elif 条件A2:
...
else:
...
elif 条件B:
B成立,执行此缩进中的代码
...
else:
上述都不成立
  • 条件嵌套示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    username = input('请输入用户名:')
    password = input('请输入密码:')

    if username == 'zhangyafei':
    if password == '123':
    print('用户名密码正确')
    else:
    print('密码输入错误')
    else:
    print('账号输入错误')
  • 模拟10086客服

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    print('欢迎致电10086,我们提供了如下服务:1.话费相关;2.业务办理;3.人工服务')

    choice = input('请选择服务序号:')

    if choice == '1':
    print('花费相关业务')
    cost = input('查询花费请按1;交话费请按2')
    if cost == '1':
    print('话费余额为100')
    elif cost == '2':
    print('交话费')
    else:
    print('输入错误')
    elif choice == '2':
    print('业务办理')
    elif choice == '3':
    print('人工服务')
    else:
    print('序号输入错误')

五、三目运算符

1
2
3
res = A if condition else B
如果条件为真:res = A
如果条件为假:res = B

另一种写法

1
res = [A,B][condition]

六、pass的作用

  • 若语句块内不执行任何操作 不能为空 必须写

  • pass代指空代码,无意义,仅仅用于表示代码块

1
2
3
4
5
name = 'xxx'
if name != 'xxx':
pass
else:
print('你是xx')
赞赏一下吧~