【精品分享】《Python编程从入门到实践》学习笔记——第五章 if语句

本系列记录的是自己学习笔记,以及一些总结,并非照搬书本,道友可进行查漏补缺,与君共勉。

本系列文章传送门
第2章 变量和简单数据类型
第3章 列表简介
第4章 操作列表
第5章 if语句
第6章 字典          未完待续
第7章 while循环 未完待续
第8章 函数         未完待续
第9章 类            未完待续
第10章 文件和异常 未完待续
项目实战



5.1 条件测试

每条if语句的核心都是一个值为True或False的表达式,这种表达式被称为条件测试

5.1.1 检查是否相等(==)

双等号两边的值相等,返回True;不等则返回False.

# 例1
car = 'bmw'
car == 'bmw'
# 输出结果
True
===============================
# 例2
car = 'audi'
car == 'bmw'
# 输出结果
False

特别解析:

  1. 一个等号(=),表示赋值,解读为“将变量car的值设置为audi”;
  2. 两个等号(==),表示判断,检查两边值是否相等;
  3. 双等号判断两边是否相等时严格区分大小写;
# 大小写不同将返回False
car = 'Audi'
car == 'audi'
# 输出结果
False
  1. 若只想检查变量的值,不考虑大小写,可将变量的值先转换为小写,再进行比较。
car = 'Audi'
car.lower() == 'audi'
# 输出结果
True

5.1.2 检查是否不相等(!=)

answer = 17
if answer != 42:
    print('That is not the correct answer, please try again!')

特别解析
数字的比较,还有大于 > ,大于等于 >= ,小于 < ,小于不等于 <=

5.1.3 检查多个条件

(一)且的关系用and
多个条件同时满足才返回True!

age = 20
sex = 'man'
if age >= 18 and sex == 'man':
    print("You're a grown man.")
    
# 输出结果
You're a grown man.

(二)或的关系用or
多个条件只要有一个满足即返回True!

phone = 'huawei'
if phone == 'huawei' or phone == 'xiaomi':
    print("This is a Mobile phone.")
    
# 输出结果
This is a Mobile phone.

5.1.4 检查特定值在列表中(in)、不在列表中(not in)

# 在列表中返回True
requested_toppings = ['mushrooms', 'onions', 'pineapple']
'mushrooms' in requested_toppings
# 输出结果
True

# 不在列表中返回True
'pepperoni' in requested_toppings
# 输出结果
False
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.

5.2 if语句

很简单,感觉没太多好说的,就举例说明各种if表达式吧。

5.2.1 简单的if语句

注意if后要有冒号, 后面的代码块要缩进

age = 19
if age >= 18:
    print("You are old enough to vote!")

# 输出结果
You are old enough to vote!

5.2.2 if-else 语句

注意if,else后要有冒号,后面的代码块要缩进

age = 17
if age >= 18:
    print("You are old enough to vote!")
    print("Have you registered to vote yet?")
else:
    print("Sorry, you are too young to vote.")
    print("Please register to vote as soon as you turn 18!")
    
# 输出结果
Sorry, you are too young to vote.
Please register to vote as soon as you turn 18!

5.2.3 if-elif-else语句

注意if,elif,else后要有冒号,后面代码块要缩进

age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 5
else:
    price = 10
print("Your admission cost is $" + str(price) + ".")

# 输出结果
Your admission cost is $5.

5.2.4 多个elif代码块

age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 65:
    price = 10
else:
    price = 5
print("Your admission cost is $" + str(price) + ".")

# 输出结果
Your admission cost is $5.

5.2.5 省略else代码块

else是一条包罗万象的语句,只要不满足任何if或elif中的条件测试,其中的代码就会执行,这可能会引入无效甚至恶意的数据。
如果知道最终要测试的条件,应考虑使用一个elif代码块来代替else代码块。这样,你就可以肯定,仅当满足相应的条件时,你的代码才会执行。

age = 12
if age < 4:
     price = 0
elif age < 18:
     price = 5
elif age < 65:
     price = 10
elif age >= 65:
     price = 5
print("Your admission cost is $" + str(price) + ".")

# 输出结果
Your admission cost is $5.

5.2.6 测试多个条件

if-elif-else结构功能强大,但仅适合用于只有一个条件满足的情况。有时候我们需要检查多个条件,每个条件为True时都采取相应措施。
这时就使用多个独立的简单if语句(不包含elif和else).

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-else结构;如果要运行多个代码块,就使用一系列独立的if语句。


如文章对您有帮助,感谢您的点赞+关注(^ _ ^)

福利:添加关注、评论区留言免费赠送自己学习资料

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值