简单实例:
cars = ['audi','bmw','subaru','toyata']
for car in cars:
if car == 'bmw': print(car.upper()) else: print(car.title())
条件测试:
car = 'bmw'
car == 'bmw'
返回True 区分大小写
age_0 = 20
age_1 = 18if age_0 >= 21 and age_1 >=15: and两个条件都需满足 print('True')else: print('Flase')
age_0 = 20
age_1 = 18if age_0 >= 21 or age_1 >=15: or满足一个条件即可print('True')
else: print('Flase')
shopping = ['iphone','meat','apple','ice cream'] 检查特定值是否包含在列表中,不包含则用not in
if 'iphone' in shopping:
print ('True')
条件判断:
requested_topping = 'mushrooms'
if requested_topping != 'an': print('LLL')
if语句:
简单的if语句只有一个测试和一个操作:
age = 19
if age >= 18: print('you are old boy')
if else语句:
age = 17
if age >= 18: print('you are old boy')else: print('yeoyeoyeo')
if elif else 语句:
age = 12
if age < 4: print('your admission cost is $0')elif age < 18: print('your admission cost is $5')else: print('your admission cost $10')
count = list(range(0,11))
for i in count: if i == 1: print('1st') elif i ==2: print('2nd') elif i == 3: print(str(i) + 'rd') else: print(str(i) + 'th')