一个简单的例子:
cars = ['audi','bmw','subaru','toyota']
for carin cars:
if car =="audi":
print(car.upper())
else:
print(car.title())
输出:
AUDI
Bmw
Subaru
Toyota
判断两个值是否相等:==
age == 18
判断两个值不相等 : !=
answer = 17:
if answer != 42:
print('sa')
检查多个条件 and
age_0 >=21 and age_1 >=21
检查多个条件 or
age_0 >=21 or age_1>=21
是否包含: in
是否不包含:not in
if 语句·:
bool =True
if True:
print("sa")
bool =False
if False:
print("false")
else:
print("true")
age =12
if age <4:
print("a")
elif age <18:
print("b")
else:
print("c")
嵌套列表循环:
list = ['a','b','c']
list1 = ['a','b','d']
for i in list:
if iin list1:
print("a")
else:
print("b")
print("c")