Python3.X中的条件控制

来源:互联网 发布:新浪域名国外的 编辑:程序博客网 时间:2024/05/22 15:12

Python中没有 switch-case 语句。
Python中需条件、循环等后面使用 冒号 :做结尾。
Python中使用 缩进 划分代码块,相同缩进则代表同一个代码块。
Python中使用 elif 代替 else if

可以使用比较运算符 in 和 not in 检测值是否存在区间之中。
if条件语句

>>> a = 10>>> if a > 0:...     print('Positive Number')... elif a < 0:...     print('Negative Number')... else:...     print('Zero')... Positive Number

in 和 not in 判断一个值是否存在一个区间

>>> myList = [0,1,2,3]>>> a = 2>>> if a in myList:...     print('Helle World')... Helle World

is 和 not is 判断是否是一样的

>>> myList = [0,1,2,3]>>> myTupl = (0,2,3,4)>>> if myList is myTupl:...     print('We are the same')... else:...     print('We look alike')... We look alike

操作运算符可以传递

i=10a = -10b = 10if i > a == b :#既要满足 i > a 还要满足 a == b    print('same')else:    print('alike')#输出结果alike

if 简写判断只要传入值是非空字符串、非空列表、非0数字,会判断为Ture,如果传入的值是0、{}、[]、’ ‘、” “,会判断为false

>>> x = 1>>> if x:...     print('one')... else:...     print('0')... one
原创粉丝点击