Python自学教程 5 If 语句(零基础新手小白适用型~)

5.1 一个简单示例

例子:

cars=['audi','bmw','subaru','toyota']
for car in cars:
    if car=='bmw':
        print(car.upper())
    else:
        print(car.title())

结果:

Audi
BMW
Subaru
Toyota

5.2 条件测试

5.2.1 检查是否相等

  方法:先“=”予变量,再与给定值“==”比较
>>> car='bmw'
>>> car=='bmw'
True
car='bmw'
>>> car=='audi'
False

5.2.2 检查是否相等时可以不用考虑大小写

1、两个大小写不同的值会认为不相等

>>> car='bmw'
>>> car=='Bmw'
False

2、将变量的值转换为小写,再进行比较

 car='Bmw'
>>> car.lower()=='bmw'
True

5.2.3 检查是否不相等

方法:“!=”

requested_topping='mushirooms'
if requested_topping!='anchovies':
    print('hold the anchovies')

5.2.4 比较数字

方法:“</>/<=/>=/=="

>>> age=18
>>> age==18
True
>>> age<21
True
>>> age>=19
False

5.2.5 检查多个条件

1、使用and检查条件,相当于“且”
2、使用or检查条件,相当于“或”

>>> age1=23
>>> age2=24
>>> age1>=23 and age2<=23
False
>>> (age1>=23) and(age2<=23)
False
>>> (age1>=23) or( age2<=23)
True

增强可读性可以使用括号。

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

方法:使用关键字“not in"

users=['aby','les','gay']
user='marie'
if user not in users:
	print(user.title()+", you are not welcomed")

结果:

Marie, you are not welcomed

5.2.8 布尔表达式

布尔表达式是条件测试的别名,结果为True或者False

5.3 if 语句

5.3.1 简单的if语句

方法:if conditional _test:
do something()可以print好多行,注意缩进

假设有一个表示某人年龄的变量,而想知道这个人是否够投票的年龄:

age1=18
if age1>=18:
    print("u can join")
输出:u can join

5.3.2 if-else 语句

条件测试通过时执行一个操作,在没有通过时执行另外一个操作。

例子:

age1=5
if age1>=18:
    print("u can join")
else:
    print('u cannot join')

结果:

u cannot join

5.3.3 if-elif-else结构

需要检查超过两个的情形

如下情形:
游乐园门票

  • 4岁以下free
  • 4-18岁收费5元
  • 18以上收费100
age=12
if age<4:
    print('you are free')
elif age<18:
    print('you should pay 5 yuan')
else:
    print('you should pay 100yuan')

输出:you should pay 5 yuan

为了让代码更简洁,可以不再if-elif-else代码块中打印门票的价格,而只在它后面添加一条简单的print语句

age=12
if age<4:
  price=0
elif age<18:
  price=5
else:
  price=100
print("your admission cost is "+str(price)+'.')
输出:your admission cost is 5.

5.3.4 使用多个elif代码块

可根据需要使用任意数量的elif代码块。加入上述条件中,加入给大于六十五岁以上人群半价优惠购买门票:

age=66
if age<4:
  price=0
elif age<18:
  price=5
elif age>65:
  price=50
else:
  price=100
print("your admission cost is "+str(price)+'.')
输出:your admission cost is 50.

5.3.5 省略else代码块

在一些情况下,使用elif语句来处理特定的情形更清晰。

5.3.6 测试多个条件

方法:采用一系列if语句


```python
requested_toppings=['mushrooms','extra cheese']
if 'mushrooms' in requested_toppings:
  print('adding mushrooms')
if 'pepperoni' in requested_toppings:
  print('adding pepperoni')
if 'extra cheese' in requested_toppings:
  print('adding extra cheese')
print("\nfinished making your pizza")
输出:
adding mushrooms
adding extra cheese

finished making your pizza```

如果采用if-elif代码块,则通过一个测试之后就直接输出。

5.4 使用if语句处理列表

5.4.1 检查特殊元素

比萨店,每增加一种配料打印一条消息,通过创建一个列表,在其中包含顾客点的配料,并用一个循环来指出添加到披萨中的配料,可以以及高效率编写这样的代码

requested_toppings=['mushrooms','green peppers','extra cheese']
for requested_topping in requested_toppings:
  print('adding '+requested_topping+'.')
print('\nfinished making your pizza!')
输出:adding mushrooms.
adding green peppers.
adding extra cheese.

finished making your pizza!

如果披萨店的青椒用完了,在For循环中包含一条if语句:

requested_toppings=['mushrooms','green peppers','extra cheese']
for requested_topping in requested_toppings:
  if requested_topping=='green peppers':
    print('sorry, we are out of green right now.')
  else:
    print('adding '+requested_topping+'.')
print('\nfinished making your pizza!')
输出:
adding mushrooms.
sorry, we are out of green right now.
adding extra cheese.

finished making your pizza!

5.4.2 确定列表不是空的

if语句中,将列表名用在条件表达式中时,Python将在列表至少包含一个元素时直接返回True,并在列表为空时返回False。如果requested_toppings不为空,就运行与前一个示例相同的for循环;否则,就打印一条消息。

requested_toppings=[]
if requested_toppings:
  for requested_topping in requested_toppings:
    print('adding ' + requested_topping + '.')
else:
  print('Are you sure?')
输出:Are you sure?

5.4.3 使用多个列表

使用列表和if语句确定是否能够满足顾客的多需求。

available_TOPPINGS=['mushrooms','olivws','green peppers','pepperoni','pineapple','extra cheese']
requested_toppings=['mushrooms','french fries','extra cheese']
for requested_topping in requested_toppings:
  if requested_topping in available_TOPPINGS:
    print('adding '+requested_topping)
  else:
    print('sorry, we do not have '+requested_topping)
   输出:
adding mushrooms
sorry, we do not have french fries
adding extra cheese

复盘

  1. 简单的if语句,if-else语句和if-elif-else结构
  2. for循环高效表示,对特定列表元素进行处理
  3. 多个列表运用
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值