Python的条件语句与运算符优先级详解
作者:goldensun 时间:2023-06-21 07:43:49
Python 条件语句
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
可以通过下图来简单了解条件语句的执行过程:
Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false。
Python 编程中 if 语句用于控制程序的执行,基本形式为:
if 判断条件:
执行语句……
else:
执行语句……
其中"判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围。
else 为可选语句,当需要在条件不成立时执行内容则可以执行相关语句,具体例子如下:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例1:if 基本用法
flag = False
name = 'luren'
if name == 'python': # 判断变量否为'python'
flag = True # 条件成立时设置标志为真
print 'welcome boss' # 并输出欢迎信息
else:
print name # 条件不成立时输出变量名称
输出结果为:
>>> luren # 输出结果
if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
当判断条件为多个值是,可以使用以下形式:
if 判断条件1:
执行语句1……
elif 判断条件2:
执行语句2……
elif 判断条件3:
执行语句3……
else:
执行语句4……
实例如下:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例2:elif用法
num = 5
if num == 3: # 判断num的值
print 'boss'
elif num == 2:
print 'user'
elif num == 1:
print 'worker'
elif num < 0: # 值小于零时输出
print 'error'
else:
print 'roadman' # 条件均不成立时输出
输出结果为:
>>> roadman # 输出结果
由于 python 并不支持 switch 语句,所以多个条件判断,只能用 elif 来实现,如果判断需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例3:if语句多个条件
num = 9
if num >= 0 and num <= 10: # 判断值是否在0~10之间
print 'hello'
>>> hello # 输出结果
num = 10
if num < 0 or num > 10: # 判断值是否在小于0或大于10
print 'hello'
else:
print 'undefine'
>>> undefine # 输出结果
num = 8
# 判断值是否在0~5或者10~15之间
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):
print 'hello'
else:
print 'undefine'
>>> undefine # 输出结果
当if有多个条件时可使用括号来区分判断的先后顺序,括号中的判断优先执行,此外 and 和 or 的优先级低于>(大于)、<(小于)等判断符号,即大于和小于在没有括号的情况下会比与或要优先判断。
简单的语句组
你也可以在同一行的位置上使用if条件判断语句,如下实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
var = 100
if ( var == 100 ) : print "变量 var 的值为100"
print "Good bye!"
以上代码执行输出结果如下:
变量 var 的值为100
Good bye!
Python运算符优先级
以下表格列出了从最高到最低优先级的所有运算符:
以下实例演示了Python所有运算符优先级的操作:
#!/usr/bin/python
a = 20
b = 10
c = 15
d = 5
e = 0
e = (a + b) * c / d #( 30 * 15 ) / 5
print "Value of (a + b) * c / d is ", e
e = ((a + b) * c) / d # (30 * 15 ) / 5
print "Value of ((a + b) * c) / d is ", e
e = (a + b) * (c / d); # (30) * (15/5)
print "Value of (a + b) * (c / d) is ", e
e = a + (b * c) / d; # 20 + (150/5)
print "Value of a + (b * c) / d is ", e
以上实例输出结果:
Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
Value of (a + b) * (c / d) is 90
Value of a + (b * c) / d is 50
标签:Python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python网络编程学习笔记(五):socket的一些补充
2022-02-03 23:22:48
Go Web 编程中的模板库应用指南(超详细)
2024-02-05 20:50:55
![](https://img.aspxhome.com/file/2023/7/105847_0s.png)
服务器不支持 MySql 数据库的解决方法
2024-01-15 11:44:57
关于Tensorflow分布式并行策略
2024-01-02 09:39:28
![](https://img.aspxhome.com/file/2023/3/110023_0s.jpg)
使用postman进行接口测试的方法(测试用户管理模块)
2023-03-13 15:59:11
![](https://img.aspxhome.com/file/2023/4/112094_0s.png)
将表数据生成SQL脚本的存储过程和工具
2009-01-08 15:24:00
MSSQL2005数据附加失败报错3456解决办法
2012-11-30 19:56:59
解决MySql客户端秒退问题(找不到my.ini)
2024-01-20 15:36:52
![](https://img.aspxhome.com/file/2023/9/91449_0s.jpg)
Pycharm连接远程服务器过程图解
2023-07-25 04:37:53
![](https://img.aspxhome.com/file/2023/1/88991_0s.png)
10个不为人知的Google失败作品
2008-05-24 16:56:00
![](https://img.aspxhome.com/file/UploadPic/20085/24/200852417924655s.jpg)
聊聊Druid register mbean error的问题
2022-09-15 19:09:00
![](https://img.aspxhome.com/file/2023/6/131816_0s.png)
解决pycharm 格式报错tabs和space不一致问题
2022-10-12 11:36:12
![](https://img.aspxhome.com/file/2023/7/128737_0s.jpg)
MAC系统中添加MYSQL开机启动的方法
2024-01-13 01:52:53
MySQL8.0.21安装步骤及出现问题解决方案
2024-01-22 20:43:44
![](https://img.aspxhome.com/file/2023/6/69826_0s.png)
学会javascript之迭代器
2024-04-10 16:12:40
ffmpeg+Python实现B站MP4格式音频与视频的合并示例代码
2023-02-01 00:08:57
![](https://img.aspxhome.com/file/2023/7/65887_0s.png)
php中session_id()函数详细介绍,会话id生成过程及session id长度
2023-11-24 08:01:00
Python判断文件或文件夹是否存在的三种方法
2021-04-25 21:53:06
SQL Substring提取部分字符串
2024-01-14 20:03:07
SQL Server 2000“设备激活错误”的解决方法
2024-01-12 15:30:23
![](https://img.aspxhome.com/file/2023/3/94443_0s.jpg)