cars =['audi','bmw','subaru','toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
car = 'Audi'
if car=='Audi':#检查是否相等时区分大小写
print('true')
if car.lower()=='audi':
print('true')
else:
print('false')
print(car)
'''
数字比较:>,<,>=,<=,!=
检查多个条件:and,or,
'''
print('--------')
age_0 = 22
age_1 = 18
if age_0 >= 21 and age_1 >= 21:
print('true')
else:
print('false')
age_1=22
if age_0 >= 21 and age_1 >= 21:
print('true')
else:
print('false')
print('--------')
if age_0 >= 21 or age_1 >= 21:
print('true')
'''
判定特定值是否包含在列表中:in
'''
print('--------')
foods = ['pizza','falafel','carrot cake']
if 'pizza' in foods:
print('true')
else:
print('false')
'''
检查特定值是否包含在列表中:not in
'''
print('--------')
foods = ['pizza','falafel','carrot cake']
food = 'beef'
if food not in foods:
print(food.title()+' is not in the mune')
'''
简单条件语句:
1、if condition_test:
do something
2、if else语句:
if condition_test:
do something
else:
do something
3、if-elif-else语句:
if condition_test:
do something
elif condition_test:
do something
else:
do something
4、使用多个elif 代码块
if condition_test:
do something
elif condition_test:
do something
elif condition_test:
do something
else:
do something
5、省略else的情况(较多个elif代码块更优)
if condition_test:
do something
elif condition_test:
do something
elif condition_test:
do something
6、假如要判断的语句有多条正确则用一系列if代码块实现
if condition_test:
do something
if condition_test:
do something
if condition_test:
do something
'''
print('--------')
age = 19
if age >= 18:
print('you are old enought to vote')
print('--------')
age = 17
if age >= 18:
print('you are old enought to vote')
else:
print('sorry you are to youngto vote')
print('--------')
age = 15
if age <4:
print('your admission cost is 0$ ')
elif age >= 4 and age < 18:
elif age < 18:#相当于前面的if条件未通过的情况下又附加了一个条件。
#相当于if age >= 4 and age < 18:语句。
print('your admission cost is '+str(5)+'')
或者:
print('--------')
age = 15
if age < 4:
price = 0
elif age < 18:
price = 5
else:
price = 10
print('your admission cost is '+str(price)+'')
print('--------')
age = 32
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
elif age >= 65:
price = 0
print('your admission cost is '+str(price)+'$')
print('--------')
cars =['audi','bmw','subaru','toyota']
for car in cars:
if car == 'audi':
print('I like '+car.title())
if car == 'bmw':
print('I like '+car.upper())
if car =='toyota':
print('I like '+car.title())
'''
检查列表不为空
'''
print('--------')
cars =[]
if cars:#表示列表中至少有一个元素时执行if语句
for car in cars:#运行循环语句时检查列表是否为空很重要
print('I like '+ car.title()+'!')
else:
print('It is plain car')
'''
使用多个列表
'''
print('--------')
available_toppings = ['mushroom','olives','green peppers','pepperoni','pineapple','extra cheese']
request_toppings = ['mushroom','french fries','extra cheese']
for request_topping in request_toppings:
if request_topping in available_toppings:
print('Adding '+request_topping.title()+'.')
else:
print('sorry we don`t have '+request_topping.title()+'.')