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数据类型详解(四)字典:dict
May 12 Python
Python实现获取本地及远程图片大小的方法示例
Jul 21 Python
Python中实现单例模式的n种方式和原理
Nov 14 Python
Python 通过调用接口获取公交信息的实例
Dec 17 Python
计算机二级python学习教程(3) python语言基本数据类型
May 16 Python
为什么从Python 3.6开始字典有序并效率更高
Jul 15 Python
Python企业编码生成系统总体系统设计概述
Jul 26 Python
解决Python在导入文件时的FileNotFoundError问题
Apr 10 Python
Python如何爬取51cto数据并存入MySQL
Aug 25 Python
在pycharm中使用pipenv创建虚拟环境和安装django的详细教程
Nov 30 Python
python 获取计算机的网卡信息
Feb 18 Python
基于Python实现射击小游戏的制作
Apr 06 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实现的SESSION类
2014/12/02 PHP
php实现Session存储到Redis
2015/11/11 PHP
十分钟打造AutoComplete自动完成效果代码
2009/12/26 Javascript
用js读、写、删除Cookie代码分享及详细注释说明
2014/06/05 Javascript
jquery网页回到顶部效果(图标渐隐,自写)
2014/06/16 Javascript
浅析Javascript中“==”与“===”的区别
2014/12/23 Javascript
JQuery中绑定事件(bind())和移除事件(unbind())
2015/02/27 Javascript
jQuery基于ajax()使用serialize()提交form数据的方法
2015/12/08 Javascript
jquery取消事件冒泡的三种方法(推荐)
2016/05/28 Javascript
JS实现快速比较两个字符串中包含有相同数字的方法
2017/09/11 Javascript
实例分析Array.from(arr)与[...arr]到底有何不同
2019/04/09 Javascript
js实现简单掷骰子小游戏
2019/10/24 Javascript
JavaScript 装逼指南(js另类写法)
2020/05/10 Javascript
NestJs使用Mongoose对MongoDB操作的方法
2021/02/22 Javascript
[05:08]顺网杯ISS-DOTA2赛歌 少女偶像Lunar青春演绎
2013/12/05 DOTA
[03:20]次级联赛厮杀超职业 现超级兵对拆世纪大战
2014/10/30 DOTA
Python应用03 使用PyQT制作视频播放器实例
2016/12/07 Python
Python 中包/模块的 `import` 操作代码
2019/04/22 Python
Python中请不要再用re.compile了
2019/06/30 Python
pytorch-神经网络拟合曲线实例
2020/01/15 Python
python主要用于哪些方向
2020/07/05 Python
Python读取图像并显示灰度图的实现
2020/12/01 Python
python基于Kivy写一个图形桌面时钟程序
2021/01/28 Python
世界领先的26岁以下学生和青少年旅行预订网站:StudentUniverse
2018/07/01 全球购物
中东地区最大的奢侈品市场:The Luxury Closet
2019/04/09 全球购物
巴西24小时在线药房:Droga Raia
2020/05/12 全球购物
铭立家具面试题
2012/12/06 面试题
Linux内核产生并发的原因
2016/11/08 面试题
自动化毕业生专业自荐书范文
2014/02/04 职场文书
初中同学聚会感言
2014/02/11 职场文书
法制教育演讲稿
2014/09/10 职场文书
群众路线教育实践活动学习笔记内容
2014/11/06 职场文书
超市采购员岗位职责
2015/04/07 职场文书
尝试使用Python爬取城市租房信息
2022/04/12 Python
mysql中关键词exists的用法实例详解
2022/06/10 MySQL
Hive导入csv文件示例
2022/06/25 数据库