Python中的if、else、elif语句用法简明讲解


Posted in Python onMarch 11, 2016

下面我们学习if语句,输入下面的代码,确保能够正确运行。

people = 20 
cats = 30 
dogs = 15 
 
 
if people < cats: 
  print "Too many cats! The world is doomed!" 
 
 
if people > cats: 
  print "Not many cats! The world is saved!" 
 
 
if people < dogs: 
  print "The world is drooled on!" 
 
 
if people > dogs: 
  print "The world is dry!" 
 
 
dogs += 5 
 
 
if people >= dogs: 
  print "People are greater than or equal to dogs." 
 
 
if people <= dogs: 
  print "People are less than or equal to dogs." 
 
 
if people == dogs: 
  print "People are dogs."

运行结果

root@he-desktop:~/mystuff# python ex29.py
Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.

加分练习
通过上面的练习,我们自己猜测一下if语句的作用,用自己的话回答下面的问题。
1. 你认为if对它下面的代码做了什么?
判断为True就执行它下面的代码,否则不执行。

2. 为什么if下面的代码要缩进4个空格?
为了表示这些代码属于if判断下包括的代码。

3. 如果不缩进会发生什么?
会提示一个缩进错误。

4. 你可以从第27节中拿一些布尔表达式来做if判断吗?

5. 改变people,dogs,cats变量的值,看看会发生什么?

答案:
1. if语句下面的代码是if的一个分支。就像书里的一个章节,你选择了这章就会跳到这里阅读。这个if语句就像是说:“如果布尔判断为True,就执行下面的代码,否则跳过这些代码”。

2. 用冒号结束一个语句就是要告诉python,我要开始一个新的代码段了。缩进4个空格就是说,这些代码是包含在这个代码段中的,和函数的使用一样。

3. 不缩进会报错,python规定冒号后面语句必须有缩进。

4. 可以,而且可以是复杂的语句。

5. 修改变量的值后,判断语句就会相应的变True或者False,然后输出不同的语句。

比较我的答案和你自己的答案,确保你能理解代码块这个概念,因为这个对于下面的练习非常重要。

输入下面的代码,运行它:

people = 30 
cars = 40 
buses = 15 
 
 
if cars > people: 
  print "We should take the cars." 
elif cars < people: 
  print "We should not take the cars." 
else: 
  print "We can't dicide." 
 
 
if buses > cars: 
  print "That's too many buses." 
elif buses < cars: 
  print "Maybe we could take the buses." 
else: 
  print "We still can't decide." 
 
 
if people > buses: 
  print "Alright, let's just take the buses." 
else: 
  print "Fine, let's stay home then."

运行结果

root@he-desktop:~/mystuff# python ex30.py
We should take the cars.
Maybe we could take the buses.
Alright, let's just take the buses.
Python 相关文章推荐
python发送arp欺骗攻击代码分析
Jan 16 Python
使用Python神器对付12306变态验证码
Jan 05 Python
python+pyqt实现右下角弹出框
Oct 26 Python
python简单实现操作Mysql数据库
Jan 29 Python
详谈pandas中agg函数和apply函数的区别
Apr 20 Python
解决python3 网络请求路径包含中文的问题
May 10 Python
python自动化报告的输出用例详解
May 30 Python
python3实现多线程聊天室
Dec 12 Python
python整小时 整天时间戳获取算法示例
Feb 20 Python
关于不懂Chromedriver如何配置环境变量问题解决方法
Jun 12 Python
Numpy与Pytorch 矩阵操作方式
Dec 27 Python
tensorflow tf.train.batch之数据批量读取方式
Jan 20 Python
使用Python读写文本文件及编写简单的文本编辑器
Mar 11 #Python
简单讲解Python中的数字类型及基本的数学计算
Mar 11 #Python
详解Python中的变量及其命名和打印
Mar 11 #Python
Python基本语法经典教程
Mar 11 #Python
Python使用PIL库实现验证码图片的方法
Mar 11 #Python
Python2.x利用commands模块执行Linux shell命令
Mar 11 #Python
Python实现列表转换成字典数据结构的方法
Mar 11 #Python
You might like
php中生成随机密码的自定义函数代码
2013/10/21 PHP
php设计模式之单例模式使用示例
2014/01/20 PHP
百度ping方法使用示例 自动ping百度
2014/01/26 PHP
phpStorm2020 注册码
2020/09/17 PHP
How to Auto Include a Javascript File
2007/02/02 Javascript
在第一个input框内输入内容.textarea自动得到第一个文件框的值的javascript代码
2007/04/20 Javascript
JQuery调用WebServices的方法和4个实例
2014/05/06 Javascript
JavaScript插件化开发教程 (二)
2015/01/27 Javascript
JavaScript比较两个对象是否相等的方法
2015/02/06 Javascript
jQuery使用EasyUi实现三级联动下拉框效果
2017/03/08 Javascript
layui文件上传实现代码
2017/05/20 Javascript
JS 调试中常见的报错问题解决方法
2017/05/20 Javascript
anime.js 实现带有描边动画效果的复选框(推荐)
2017/12/24 Javascript
JS常见DOM节点操作示例【创建 ,插入,删除,复制,查找】
2018/05/14 Javascript
JS正则表达式常见用法实例详解
2018/06/19 Javascript
微信小程序日历效果
2018/12/29 Javascript
为什么要使用Vuex的介绍
2019/01/19 Javascript
js实现删除li标签一行内容
2019/04/16 Javascript
[03:03]2014DOTA2国际邀请赛 EG战队专访
2014/07/12 DOTA
pygame学习笔记(1):矩形、圆型画图实例
2015/04/15 Python
python编写爬虫小程序
2015/05/14 Python
Python的爬虫包Beautiful Soup中用正则表达式来搜索
2016/01/20 Python
django中的HTML控件及参数传递方法
2018/03/20 Python
python pygame模块编写飞机大战
2018/11/20 Python
Django中使用Whoosh进行全文检索的方法
2019/03/31 Python
CentOS7安装Python3的教程详解
2019/04/10 Python
PHP统计代码行数的小代码
2019/09/19 Python
在pandas中遍历DataFrame行的实现方法
2019/10/23 Python
python 错误处理 assert详解
2020/04/20 Python
澳大利亚汽车零部件、音响及配件超市:Automotive Superstore
2018/06/19 全球购物
彪马日本官网:PUMA日本
2019/01/31 全球购物
财务会计大学生自我评价
2014/04/09 职场文书
亲子活动总结
2014/04/26 职场文书
工会优秀工作者事迹
2014/08/17 职场文书
金榜题名主持词
2015/07/02 职场文书
JAVA SpringMVC实现自定义拦截器
2022/03/16 Python