条件测试

1. 检查是否相等

一个等号表示赋值,两个等号用于判断等号左右两边是否相等,返回值为True或者False.

2. 检查是否相等是需考虑大小写

大小写不同的值视为不相等,例如继续写入代码:car == 'Bmw',返回:False

此时就可引用lower()或者upper()函数进行字符串大小写的转换,方便比较。

3. 检查是否不相等

与判断是否相等类似,不过是将第一个等号用感叹号替换,即:!= 表示不等运算符。

4. 其他条件判断

除了等于和不等之外,还可比较两者是否大于(>)、大于等于(>=)、小于(<)、小于等于(<=)。

5. 判断多个条件

可用关键字and或or将两个单独的条件判断合二为一:

6. 检查特定值是否在列表中

7. 布尔表达式

给变量赋值为True或False,eg: edit = True

if语句

1. 简单的if语句

# 判断是否达到投票的年龄
age = 19
if age >= 18:
print('You are old enough to vote!')

2. if-else语句

age = 17
if age >= 18:
print('You are old enough to vote!')
else:
print('Sorry, you are too young to vote yet.')

3. if-elif-else语句

age = 12
if age < 4:
print('Your admission cost is 0 yuan.')
elif age < 18:
print('Your admission cost is 5 yuan.')
else:
print('Your admission cost is 10 yuan.')

上述的代码中有3条打印语句,有点繁琐,也可简化为:

if age < 4:
price = 0
elif age < 18:
price = 5
else:
price = 10
print('Your admission cost is ' + str(price) + ' yuan.') # 用str()将数字转换为字符型,否则会因类型不一致报错

4. 使用多个elif代码块

if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 5

5. else代码块可以省略

用代码 elif age >= 65: 替换 else:

6. 多个条件

require_foods = ['pizza', 'falafel', 'carrot cake']
if 'pizza' in require_foods:
print('Adding pizza')
if 'falafel' in require_foods:
print('Adding falafel')
elif 'carrot cake' in require_foods:
print('Adding carrot cake')
print('This is you need.')

运行结果:

Adding pizza
Adding falafel
This is you need.

代码块中有多个if语句时,每个if语句都执行;但若是if-elif-else结构,代码运行时从前往后依次执行,一旦有条件满足,将不再执行后边的判断语句。

if语句处理列表

1. 检查特殊元素

for require_food in require_foods:
if require_food == 'carrot cake': # 判断需求是否存在
print('Sorry, we are out of carrot cake now.') # 打印供给不足
else:
print('Adding ' + require_food)

判断需求是否存在,存在则添加,不存在则抱歉。

2. 确定列表是否为空

require_foods = []
if require_foods:
for require_food in require_foods:
print('Adding ' + require_food)
else:
print('Are you sure nothing you want?')

运行结果:

Are you sure nothing you want?

没有需求时确认一下

3. 使用多个列表

menu_lists = ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
want_eats = ['falafel', 'carrot cake', 'ice cream']
for want_eat in want_eats:
if want_eat in menu_lists:
print('Adding ' + want_eat)
else:
print('Sorry, we does not have ' + want_eat + '.')

点餐时添加菜单中有的,对于没有的表示抱歉。

 

最新文章

  1. 【LeetCode】House Robber III(337)
  2. Eclipse svn插件包
  3. select接收后台返回值的解决方案
  4. So you want to be a 2n-aire?[HDU1145]
  5. Service代码示例
  6. UVALive - 7374 Racing Gems 二维非递减子序列
  7. .NET生成静态页面例子
  8. Monkey log分析说明
  9. genymotion 模拟器 真是牛叉了 速度超快啊!!! 不解释了!建议大家速度去体验一把吧!
  10. 25_Downloading An Image
  11. SOCKET网络编程细节问题(2)
  12. 第九十四节,html5+css3移动手机端流体布局,旅游部分,媒体查询
  13. Java 水仙花数
  14. VM虚拟机链接克隆及linux eth0网卡的快速设置方法
  15. servlet的url-pattern的缺省匹配【&lt;url-pattern&gt;/&lt;url-pattern&gt;】
  16. Django 路由报错友好提示
  17. 洛谷.2325.[SCOI2005]王室联邦(贪心)
  18. 1.7Oob 构造方法
  19. Structs复习 开始 第一个helloworld项目
  20. 微信小程序-获取当前城市位置及再次授权地理位置

热门文章

  1. CRMEB中因为重写规则导致的服务器异常和404之解决办法
  2. [转]重命名PostgreSQL数据库
  3. CMU Database Systems - Storage and BufferPool
  4. windows mysql绿色版配置Mysql5.7.X
  5. H2数据库函数及数据类型概述
  6. Java Web J2EE下的两大框架SSH和SSM对比
  7. 000 基于Spring boot发送邮件
  8. Android Support v4、v7、v13、v14、v17的区别和应用场景
  9. java 数据相除
  10. C++17 std::shared_mutex的替代方案boost::shared_mutex