python 三元运算符使用解析
作者:手可摘星辰。 时间:2021-05-02 17:03:28
三元运算又称三目运算,是对简单的条件语句的简写
简单条件语句:
if 条件成立:
val = 1
else:
val = 2
改成三元运算:
val = 1 if 条件成立 else 2
举例:
a = 2
b = 5
val = a if a > b else b
print(val) # 5
val = a if a < 3 else b
print(val) # 2
来源:https://www.cnblogs.com/wangdianchao/p/11459421.html
标签:python,三元,运算,符
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python使用正则表达式匹配字符串开头并打印示例
2021-07-02 00:52:13
Python数据分析之获取双色球历史信息的方法示例
2022-10-05 21:16:34
Pytorch实现各种2d卷积示例
2023-06-20 16:17:29
Python基于smtplib模块发送邮件代码实例
2022-09-18 11:07:49
使用pandas实现连续数据的离散化处理方式(分箱操作)
2023-08-28 21:45:51
Python+OpenCV图像处理——实现直线检测
2021-05-15 20:13:29
![](https://img.aspxhome.com/file/2023/1/107861_0s.png)
Python中Unittest框架的具体使用
2023-02-20 11:41:09
PHP5.6读写excel表格文件操作示例
2023-11-21 15:03:21
页面加载对访问的影响
2009-10-30 18:54:00
![](https://img.aspxhome.com/file/UploadPic/200910/30/speed_taobaoued-30s.png)
Python Pandas处理CSV文件的常用技巧分享
2022-06-18 12:56:19
![](https://img.aspxhome.com/file/2023/1/99621_0s.png)
Persits AspJpeg 1.8+ 轻松实现透明文字去锯齿水印
2009-03-20 14:03:00
Python Matplotlib绘制箱线图boxplot()函数详解
2021-03-09 11:16:07
![](https://img.aspxhome.com/file/2023/2/97222_0s.png)
使用python tkinter开发一个爬取B站直播弹幕工具的实现代码
2021-06-02 19:15:06
![](https://img.aspxhome.com/file/2023/5/81495_0s.png)
Mootools 1.2教程(21)——类(二)
2008-12-28 20:58:00
python可以美化表格数据输出结果的两个工具
2022-06-05 02:33:32
Python中的优先队列(priority queue)和堆(heap)
2023-11-22 05:40:03
![](https://img.aspxhome.com/file/2023/0/65900_0s.png)
对python列表里的字典元素去重方法详解
2023-09-20 13:45:47
python如何正确的操作字符串
2023-12-28 02:46:30
Python实现人脸识别的详细图文教程
2022-12-28 04:45:53
![](https://img.aspxhome.com/file/2023/4/85224_0s.png)
一个月入门Python爬虫学习,轻松爬取大规模数据
2022-03-10 11:04:26
![](https://img.aspxhome.com/file/2023/8/101148_0s.jpg)