python3之if语句介绍

x33g5p2x  于2021-03-13 发布在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(419)

语句快介绍

语句快并非一种语句,是通过缩进形成的语句集合;
可以使用的缩进符:TAb建(相当于四个空格),4个空格,8个空格,可以成倍增加形成嵌套语句快;
一般来说使用四个空格是最规范的。
功能:语句快是在条件为真时执行(if)或执行多次(循环)的一组语句;

  1. #伪代码
  2. this is a line
  3. this is a condition:
  4. this is a block
  5. .....
  6. there wo escaped the inner block

条件和条件语句

布尔型介绍

假(false):会被看做假的值:
FALSE ,None ,0 ,‘ ’ (没有空格) ,“ ” (没有空格) ,() ,[] ,{}
真(true):其他的都会被判定为真:
测试如下:

  1. #假
  2. >>> bool(False)
  3. False
  4. >>> bool(None)
  5. False
  6. >>> bool('')
  7. False
  8. >>> bool("")
  9. False
  10. >>> bool()
  11. False
  12. >>> bool([])
  13. False
  14. >>> bool(())
  15. False
  16. >>> bool({})
  17. False
  18. >>>
  19. #真
  20. >>> bool(True)
  21. True
  22. >>> bool('ddd')
  23. True
  24. >>> bool(1)
  25. True
  26. >>> bool([1])
  27. True
  28. >>> bool((1))
  29. True
  30. >>> bool({1})
  31. True
  32. >>>

条件执行和if语句

if语句

if语句可以实现条件执行,如果条件判定为真,则后面的语句块执行,如果条件为假,语句块就不会被执行。

  1. #如果用户输入:0-9 就打印True
  2. >>> x=int(input("Please enter an integer in 0-9 "))
  3. Please enter an integer in 0-9 6
  4. if 0<x<9:
  5. print(True)
  6. True

else语句

else子句(之所以叫子句,是因为它不是独立的语句,而只能作为if语句的一部分)当条件不满足时执行;

  1. #如果用户输入:0-9 就打印True,不在之类输出 False
  2. >>> x=int(input("Please enter an integer in 0-9 "))
  3. Please enter an integer in 0-9 11
  4. >>> if 0<x<9:
  5. ... print(True)
  6. ... else:
  7. ... print(False)
  8. ...
  9. False

elif语句

如果需要检查多个条件,就可以使用elif,它是“else if”的简写,也是if和else子句的联合使用——也就是具有条件的else子句。

  1. #如果用户输入在0-9:就打印in 0-9 ,否则如果输出大于9:就打印 >9,否则打印:<0
  2. >>> x=int(input("Please enter an integer in 0-9 "))
  3. Please enter an integer in 0-9 -1
  4. >>> if 0<x<9:
  5. ... print("in 0-9")
  6. ... elif x>9:
  7. ... print(">9")
  8. ... else:
  9. ... print("<0")
  10. ...
  11. <0
  12. >>>

嵌套if语句

if语句里可以嵌套使用if语句:

  1. x=int(input("Please enter an integer in 0-9 "))
  2. if x>=0:
  3. if x>9:
  4. print("x>9")
  5. elif x==0:
  6. print("x==0")
  7. else:
  8. print('x<0')
  9. Please enter an integer in 0-9 0
  10. x==0

更复杂的条件:

条件运算符一览:

  1. #比较运算符
  2. x==y x等于y
  3. x<y x小于y
  4. x>y x大于y
  5. x>=y x大于等于y
  6. x<=y x小于等于y
  7. x!=y x不等于y
  8. a<x<b x大于a小于b
  9. #同一性运算符
  10. x is y xy是同一个对象
  11. x is not y xy是不同的对象
  12. #成员资格运算符
  13. x in y xy容器的成员
  14. x not in y x不是y容器的成员

同一性运算符说明:
看以来和==一样实际是时不同的,只有两个绑定在相同的对象时才是真(当然当为数值变量时是和==相同的):

  1. #a与b不时指向同一对象
  2. >>> a=[1,2]
  3. >>> b=[1,2]
  4. >>> a is b
  5. False
  6. #a与b指向同一对象
  7. >>> a=b=[1,2]
  8. >>> a is b
  9. True

in成员资格运算符说明

  1. #成员在序列中返回真
  2. >>> a=[1,2,3]
  3. >>> b=1
  4. >>> c=4
  5. >>> b in a
  6. True
  7. >>> c in a
  8. False
  9. >>>

逻辑运算符:

and :x and y 返回的结果是决定表达式结果的值。如果 x 为真,则 y 决定结果,返回 y ;如果 x 为假,x 决定了结果为假,返回 x。
or :x or y 跟 and 一样都是返回决定表达式结果的值。
not : 返回表达式结果的“相反的值”。如果表达式结果为真,则返回false;如果表达式结果为假,则返回true。

  1. >>> a=1
  2. >>> b=2
  3. >>> a and b
  4. 2
  5. >>> a=1
  6. >>> b=0
  7. >>> a and b
  8. 0
  9. >>> a or b
  10. 1
  11. >>> not b
  12. True
  13. >>>

断言简介

if语句有个非常有用的近亲,其工作方式多少有点像下面这样(伪代码):
if not condition:
crash program
这样做是因为与其让程序在晚些时候崩溃,不如在错误条件出现时直接让它崩溃。一般来说,你可以要求某些条件必须为真。语句中使用的关键字为assert。
如果需要确保程序中的某个条件一定为真才能让程序正常工作的话,assert语句就有用了,可以在程序中置入检查点:
条件后可以添加字符串(用逗号把条件和字符串隔开),用来解释断言:

  1. >>> age=-1
  2. >>> assert 0<age<150,"The age must be realistic"
  3. Traceback (most recent call last):
  4. File "<stdin>", line 1, in <module>
  5. AssertionError: The age must be realistic
  6. >>>
  7. >>> age=10
  8. >>> assert 0<age<150,"The age must be realistic"
  9. >>>

相关文章