• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中的if、else、elif语句用法简明讲解

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python if elif else,if elif else,shell if elif else,if elif else endif,elif else等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

下面我们学习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." 
</div>


运行结果

root@he-desktop:~/mystuff# python ex29.py 
</div>
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.

</div>

加分练习
通过上面的练习,我们自己猜测一下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." 
</div>


运行结果

root@he-desktop:~/mystuff# python ex30.py 
</div>
We should take the cars.
Maybe we could take the buses.
Alright, let's just take the buses.
</div> </div>

您可能想查找下面的文章:

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

相关文章

  • 使用PyCharm配合部署Python的Django框架的配置纪实
  • 使用Python解析JSON数据的基本方法
  • Python二分法搜索算法实例分析
  • 一个基于flask的web应用诞生 flask和mysql相连(4)
  • Python黑魔法@property装饰器的使用技巧解析
  • python通过pil为png图片填充上背景颜色的方法
  • python定时采集摄像头图像上传ftp服务器功能实现
  • Python创建模块及模块导入的方法
  • Python import用法以及与from...import的区别
  • python实现查询IP地址所在地

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python获取外网ip地址的方法总结
    • Django Highcharts制作图表
    • Python读写txt文本文件的操作方法全解析
    • Python通过解析网页实现看报程序的方法
    • tensorflow如何继续训练之前保存的模型实例
    • Python实现将DOC文档转换为PDF的方法
    • 使用Nginx+uWsgi实现Python的Django框架站点动静分离
    • Python运算符重载用法实例
    • 详解Python的Django框架中Manager方法的使用
    • Python使用稀疏矩阵节省内存实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有