Python——条件语句(if 语句)

一. 语句块

语句块是一组满足一定条件时执行一次或多次的语句。

语句块的创建方式是在代码前放置空格缩进。

同一段语句块中每行语句都要保持同样的缩进。

在 Python 中,冒号(:)用来标识语句块的开始,语句块中每一个语句都要缩进(缩进量相同)。当退回到和已经闭合的块一样的缩进量时,表示当前语句块已经结束。

二. 布尔变量

标准值 False 和 None 、所有类型的数字 0 (包括浮点型、长整型和其他类型)、空序列(如空字符串、空元组和空列表)以及空字典都为假。其他值都为真。

假值: False、None、0、""、()、[]、{}

【注】

  • 在 Python 中,True 和 1 等价,False 和 0 等价。
  • false 布尔值为真
  • 布尔值 True 和 False 属于布尔类型, bool 函数可以用来转换其他值。
  • bool 函数做 boolean 值转换。
>>> True
True
>>> true
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>>
>>>
>>> False
False
>>> false
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'false' is not defined
>>>
>>>
>>>
>>> # 在 Python 中,True 和 1 等价,False 和 0 等价。
...
>>> True == 1
True
>>> False ==0
True
>>> True + False +2
3
>>>
>>> bool('zth')
True
>>> bool()
False
>>> bool('')
False
>>> bool(0)
False
>>> bool([])
False
>>> bool(None)
False

三. 条件语句

1. if 语句 

语法格式:

        if <判断条件>:

                 <条件语句块……>

当“判断条件”为真(True)时,执行条件语句块;反之,则不执行。

条件语句块可以为多行,以缩进来区分表示同一范围。

流程图:

                                                                    这里写图片描述

sum1 = 100

if sum1 > 90 :
    print("%d 大于  90"%sum1)

执行结果如下:

100 大于  90

2. if…else 语句

语法格式:

if <判断条件>:

        <执行语句1……>

else:

        <执行语句2……>

当“判断条件”为 True 时,执行语句1;否则,执行语句2。

流程图:

                                                                      

sum1 = 80

if sum1 > 90 :
    print("%d 大于  90"%sum1)
else :
    print("%d 小于  90"%sum1)

执行结果:

80 小于  90

3. if…elif…else 语句

语法格式:

if <判断条件1>:

        <执行语句1……>

elif <判断条件2>:

        <执行语句2……>

else:

        <执行语句3……>

流程图:

                                                               

【注】

  • elif 是 else if 的缩写。
  • 如果 if (或 elif)的条件为 True,则执行对应的“执行语句”,若为 False,则检查下一个 elif 的状态,依次进行。。。倘若所有条件都为 False,则执行 else 中的语句
  •  if 和 else 只能有一个,但 elif 可以有多个,if … elif … else 中只有一个语句块可以根据条件来执行。
sum1 = 72

if sum1 > 90 :
    print("%d 大于  90"%sum1)
elif( 70<sum1<90):
     print("%d 大于  70 小于 90"%sum1)
elif( 50<sum1<70):
     print("%d 大于  50 小于 70"%sum1)
else :
    print("%d 小于  90"%sum1)

执行结果:

72 大于  70 小于 90

4. 嵌套 if 语句

把一个  if … elif … else 语句加入至另一个 if … elif … else 语句中,这被称为嵌套。

num = 10
if num%2==0:
    if num%3==0:
        print ("你输入的数字可以整除 2 和 3")
    elif num%4==0:
        print ("你输入的数字可以整除 2 和 4")
    else:
        print ("你输入的数字可以整除 2,但不能整除 3 和 4")
else:
    if num%3==0:
        print ("你输入的数字可以整除 3,但不能整除 2")
    else:
        print  ("你输入的数字不能整除 2 和 3")

执行结果:

你输入的数字可以整除 2,但不能整除 3 和 4

四. 断言

assert断言语句用来声明某个条件是真的,其作用是测试一个条件(condition)是否成立,如果不成立,则抛出异常。

assert一般用法:

       assert   condition

如果 condition为 false,就  raise  一个 AssertionError出来。逻辑上等同于:
if not condition:
    raise AssertionError()


另一种用法:
assert condition,expression

如果  condition  为  false,就  raise一个描述为   expression 的 AssertionError出来。逻辑上等同于:
if not condition:
        raise AssertionError(expression)

 

>>> a = 1
>>> b = 2
>>>
>>>
>>> assert (a < b )
>>>
>>> assert (a > b )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>>
>>> assert (a > b ),'{0} is not bigger than {1}'.format(a,b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: 1 is not bigger than 2

猜你喜欢

转载自blog.csdn.net/qq_41573234/article/details/81563779