# 条件测试
names=['athy','irene','gattuso','ash','mw']for name in names:if name =='mw':print(name.upper())else:print(name.title())# 检查是否相等时不考虑大小写
name ='Athy'
name.lower()=='athy'if'athy'!=name.lower():print("1")else:print("0")# 比较数字# 使用and检查多个条件
age_0=18
age_1=37if(age_0<=35)and(age_1<=35):print("1")else:print("0")# 使用or检查多个条件
age_0=18
age_1=37if(age_0<=35)or(age_1<=35):print("1\n")else:print("0\n")# 检查特定值是否包含在列表中
ages =(1,2,3,4)if1in ages:print("YES")
names=['athy','irene','gattuso','ash','mw']if'athy'in names:print("YES\n")# 检查特定值是否不包含在列表中
names=['athy','irene','gattuso','ash','mw']
user ='arnold'if user notin names:print(user.title()+", you don't have right to continue .\n")# if
age =19if age >=18:print("You are old enough to vote !\n")# if-else
age =17if age >=18:print("You are old enough to vote !\n")else:print("Sorry , you are too young to vote .\n")# if-elif-else# 根据年龄段收费的游乐场# 4岁以下免费# 4~18岁shoufei$5# 18岁(含)以上收费$10
age =12if age<4:print("Your admission cost is $0.\n")elif age<18:print("Your admission cost is $5.\n")else:print("Your admission cost is $10.\n")# 多个elif代码块
age =12if age<4:
price =0elif age<18:
price =5elif age<65:
price =10else:
price =5print("Your admission cost is $"+str(price)+".\n")# 省略else代码块# elif后面一定要跟条件# else不必跟
age =12if age<4:
price =0elif age<18:
price =5elif age<65:
price =10elif age>=65:
price =5print("Your admission cost is $"+str(price)+".\n")# 测试多个条件# 如歌顾客点了2位侍者,就需要确保在服务中包含这些侍者
waiters =['alfred','arnold']if'alfred'in waiters:print("Let alfred in.")if'arnold'in waiters:print("Let arnold in.")if'anger'in waiters:print("Let anger in.")print("The waiters are all in here.\n")# 检查特殊元素
waiters =['alfred','arnold','anger']for waiter in waiters:if waiter=='alfred':print("Sorry,alfred has been hired by other master.")else:print("Let "+ waiter +" in.")print("The waiters are all in here.\n")# 确定列表不是空的# if条件中如果只有一个列表名的话会输出Ture,否则输出False
waiters =[]if waiters:for waiter in waiters:print("Let "+ waiter +" in.")print("The waiters are all in here.\n")else:print("Nobody.\n")# 使用多个列表
athy_waiters =['alfred','ash','arnold']
waiters =['alfred','anger','arnold','ash']for waiter in waiters:if waiter in athy_waiters:print("Let "+ waiter +" in.")else:print("Sorry, "+ waiter +" was no time now.")print("The athy's waiters are all in here now.")