《python编程从入门到实践》if知识点

#if-else语句格式,比如:

cars=['bmw','audi','toyota','subaru']

for car in cars:

    if car=='bmw':

        print(car.upper())

   else:

       print(car.title())

#if-else-if语句格式,比如:

age=66

if age<2:

    print("婴儿!")

elif age<4:

   print("蹒跚学步!")

elif age<13:

   print("儿童!")

elif age<20:

   print("青少年!")

elif age<65:

   print("成年人!")

else:

   print("老年人!")

#使用if检查特殊元素,比如:

requested_toppings=['mushroom','green peppers','extra cheese']

for requested_topping in requested_toppings:

    if requested_topping=='green peppers':

        print("Sorry,we are out of "+requested_topping+" right now.")

   else:

       print("Adding "+requested_topping+".")

#使用if确定列表不是空的,比如:

accounts=[]

if accounts:

   for account in accounts:

        print("Hello " + account + ",thank you for logging in again.")

  else:

       print("We need to find some users!")

#使用if多个列表,比如:

available_toopings=['mushrooms','olives','green peppers','pepperoni','pineapple','extra cheese']

requested_toppings=['mushrooms','french fries','extra cheese']

for requested_topping in requested_toppings:

    if requested_topping in available_toopings:

         print("Adding "+requested_topping+".")

   else:

         print("Sorry,we dont't have "+requested_topping+".")

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值