4.if语句

if语句

1.检查多个条件

1.1 使用and检查多个条件

and 两者同时满足

>>> age_0=22
>>> age_1=18
>>> age_0>=21 and age_1>=21
False
>>>

1.2 使用or检查多个条件

or 其中一个条件满足

age_0=22
age_1=18
print(age_0>=21 or age_1>=21)
True

2.检查特定值是否不包含在列表中

可以使用关键字not in

banned_users = ['andrew','carolina','david']
user = 'marie'
if user not in banned_users:
    print(user.title()+",you can post a response if you wish")
Marie,you can post a response if you wish        #就是因为不在其中,所以才能看到这句话

3.检查特定值是否包含在列表中

requested_toppings = ['mushrooms','onions','pineapple']
print('mushrooms' in requested_toppings)
True

4.使用if语句处理列表

4.1 确定列表是否为空

在if语句中将列表名用在条件表达式中时,python将在列表至少包含一个元素时返回True,并在列表为空时返回False.

request_toppings=[]
if request_toppings:
    for request_topping in request_toppings:
        print("Adding "+request_topping)
else:
    print("Are you sure you want a plain pizza?")
Are you sure you want a plain pizza?   #列表为空,返回False,执行else语句里面的代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值