Python 的if else 结构其实比较简单,复杂的地方其实应该算是和其他的操作合并在一起。先抛开其他的来说,只是看if else,后续会再介绍关于其他的操作,比如字符串。话不多说,直接上实例。
结构
if x == 1 and y == 1:
print x+y
elif x > 1 or y < 1:
print x
elif line == False:
print "default"
in / not in
target = "123XXXYYYZZZ"
pattern = "XXX"
noise = "CCC"
if pattern in target:
print pattern
if noise not in target:
print noise
输出结果:
XXX
CCC
循环 + if else
# for 循环
target = "test.txt"
file = open(target)
for line in file:
if x == 1 and y == 1:
continue
elif x > 1 or y < 1:
break
elif line == False:
print "default"
file.close()
"""
while 循环
"""
while True:
if x == 1 and y == 1:
continue
elif x > 1 or y < 1:
break
else:
print "default"