首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

《Python编程:从入门到实践》 第五章 笔记

第五章 IF语句

a =5

ifa ==6:

print("yes")

else:

print("no")

no

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

mvp_candidate = ["Irving","Harden","Jmaes","Durant"]

mvp ="Irving"

ifmvpinmvp_candidate:

print("True")

else:

print("False")

True

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

mvp_candidate = ["Irving","Harden","Jmaes","Durant"]

DOY ="Horford"

ifDOYnot inmvp_candidate:

print(DOY +"is the best defender of the year")

else:

print("It's OK!")

Horfordis the best defender of the year

5.2.8  布尔表达式

mvp ='Irving'

print(mvp =='Irving')

print(mvp =='James')

True

False

mvp ='Irving'

print(mvp =='Irving'.lower())

False

mvp ='Irving'

doy ='Horford'

print(mvp =='Irving'anddoy =='Horford')

True

a =5

b =7

print(a >5andb

False

mvp_candidate = ["Irving","Harden","Jmaes","Durant"]

print('Irving'inmvp_candidate)

True

mvp_candidate = ["Irving","Harden","Jmaes","Durant"]

print('Irving'not inmvp_candidate)

False

5.3.3   if-elif-else 结构

age =12

ifage ==18:

print("Bad news!You have became an audlt.")

elifage >18:

print("Keep working hard,you know what I mean.")

else:

print("You can still enjoy your time.")

You can still enjoy your time.

5.3.5  省略 else 代码块

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

age =12

ifage ==18:

print("Bad news!You have became an audlt.")

elifage >18:

print("Keep working hard,you know what I mean.")

elifage

print("You can still enjoy your time.")

You can still enjoy your time.

5.3.6  测试多个条件

Celtics = ['Irving','Harward','Horford']

if'Irving'inCeltics:

print("Irving is a Celtics!")

if'Horford'inCeltics:

print("Horford is a Celtics!")

if'Harward'inCeltics:

print("Harward is a Celtics!")

print("They will be the champion!")

Irving is a Celtics!

Horford is a Celtics!

Harward is a Celtics!

They will be the champion!

Celtics = ['Irving','Harward','Horford']

if'Irving'inCeltics:

print("Irving is a Celtics!")

elif'Horford'inCeltics:

print("Horford is a Celtics!")

elif'Harward'inCeltics:

print("Harward is a Celtics!")

print("They will be the champion!")

Irving is a Celtics!

They will be the champion!

if-if-if,在符合一个条件后他会继续去判断下一个条件

if-elif-else,在符合一个条件后不会去判断下一个条件,直接跳出循环。

5.4.2  确定列表不是空的

Celtics = []

ifCeltics:

forCelinCeltics:

print("Yes,"+ Cel +" is a Celtics!")

print("thank you for use!")

else:

print("do you know anyone in Celtics?")

do you know anyone in Celtics?

5.4.3  使用多个列表

automatic_rifle = ['M4A1','M16A4','SCAR','AK47','GROZA']

fav_automatic_fifle = ['M4A1','M16A4','SCAR']

forautomatic_rifleinfav_automatic_fifle:

ifautomatic_rifleinfav_automatic_fifle:

print("5.56")

else:

print("7.62")

print("WINNER WINNER ,CHICKEN DINNER!")

5.56

5.56

5.56

WINNER WINNER ,CHICKEN DINNER!

大于 >=

小于

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20171214G0GJDL00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券