三元运算符 python

Python ternary operator is also termed as conditional operator. This is because it can evaluate a statement with a condition being true or false.

Python三元运算符也称为条件运算符。 这是因为它可以评估条件为true或false的语句。

Python三元运算符 (Python ternary operator)

  • Python ternary operator was introduced in Python 2.5.Python 2.5中引入了Python三元运算符。
  • If used properly, ternary operator can reduce code size and increase readability of the code.如果使用正确,三元运算符可以减小代码大小并提高代码的可读性。
  • There is no special keyword for ternary operator, it’s the way of writing if-else statement that creates a ternary statement or conditional expression.三元运算符没有特殊的关键字,这是编写if-else语句的方法,该语句创建三元语句或条件表达式。

Python三元运算符语法 (Python ternary operator syntax)

Python ternary operator is written with simple syntax using if else statement.

Python三元运算符使用if else语句以简单的语法编写。

[when_true] if [condition] else [when_false]

Python三元运算符示例 (Python ternary operator example)

On the above syntax, let us quickly build up an example:

基于以上语法,让我们快速构建一个示例:

is_fast = True
car = "Ferrari" if is_fast else "Sedan"

Clearly, the presented example is a lot more readable than the usual if statement, as follows:

显然,所提供的示例比通常的if语句更具可读性,如下所示:

if is_fast:car = "Ferrari"
else:car = "Sedan"

Of course, python ternary operator made above code very small.

当然,python三元运算符使上面的代码非常小。

A very easy way to remember the order of condition is just like you think it, for example, based on above code snippet, we say “Car is Ferrari if it is fast otherwise it is Sedan”, sounds easy now, right?

记住条件顺序的一种非常简单的方法就像您认为的那样,例如,根据上述代码片段,我们说“汽车是法拉利,如果速度很快,否则它是轿车”,现在听起来很容易,对吧?

具有Tuple的Python三元运算符示例 (Python ternary operator example with Tuple)

The operation shown in the earlier section was a simple replacement for if-else conditions. Well, Ternary operators are much more powerful than those.

前面部分中显示的操作是if-else条件的简单替代。 好吧,三元运算符的功能要强大得多。

We can use ternary operator with tuple too.

我们也可以将三元运算符与元组一起使用。

(when_false, when_true)[condition]

Please note that in this syntax, False value is before True value. Consider this more complex example which works with Tuples:

请注意,在此语法中, False值在True值之前 。 考虑下面这个更适合Tuples的例子:

is_fast = True
car = ("Sedan", "Ferrari")[is_fast]

This is a very less used syntax as it might not represent clear readability like before. Also, it’s important to consider the performance of this method as this will result in both elements of the Tuple being evaluated. The earlier ternary operator didn’t result in this lag. Below image shows the output of ternary operator example code.

这是一种很少使用的语法,因为它可能不像以前那样代表清晰的可读性。 同样,考虑此方法的性能也很重要,因为这将导致对元组的两个元素进行评估。 较早的三元运算符没有导致这种滞后。 下图显示了三元运算符示例代码的输出。

Python三元运算符的优势 (Python ternary operator advantages)

Main advantages offered by ternary operator are:

三元运算符提供的主要优点是:

  • It allows us to replace simple if statements with a single line expression.它允许我们用单行表达式替换简单的if语句。
  • Increases code readability by reducing number of lines of code.通过减少代码行数来提高代码的可读性。

In this quick lesson, we studied the ternary operators in Python which were introduced in Python 2.5. Use them to shorten your code and make it more maintainable and easily readable.

在本快速课程中,我们研究了Python 2.5中引入的Python三元运算符。 使用它们可以缩短您的代码,使其更易于维护和阅读。

翻译自: https://www.journaldev.com/17225/python-ternary-operator

三元运算符 python

三元运算符 python_Python三元运算符相关推荐

  1. java if赋值语句_Java基础第3天+运算符(算术运算符、赋值运算符、比较运算符、逻辑运算符、位运算符、三元运算符)、Scanner键盘录入、if语句...

    1:运算符(掌握) (1)算术运算符 A:+,-,*,/,%,++,-- B:+的用法 a:加法 b:正号 c:字符串连接符 C:/和%的区别 数据做除法操作的时候,/取得是商,%取得是余数 D:++ ...

  2. JAVA语言运算符(算数运算符、赋值运算符、比较运算符、逻辑运算符、三元运算)

    运算符 JAVA语言中将运算符分为:算数运算符.赋值运算符.比较运算符.逻辑运算符.三元运算符. 算数运算符:算数运算符是对数字进行一系列的加减乘除等的计算,常见的算数运算符如: public cla ...

  3. 前端:JS/18/JS运算符(算术运算符,赋值运算符,字符串运算符,比较运算符,逻辑运算符,三元运算符),window.prompt()

    JS运算符 要进行各种各样的运算,就要使用不同的运算符号. 1,算术运算符:+ - * / % ++ -- + :加法运算符 - :减法运算符 * :乘法运算符 / :除法运算符 % :取余运算符,返 ...

  4. Java基础重温_02:运算符、三元运算符案例(2只老虎、3个和尚)、键盘录入(Scanner类)、控制流程语句、控制流程语句案例(奇偶数、考试奖励)

    摘要 Java基础重温_02: 运算符(算术运算.+操作.赋值运算.自增自减.关系运算(比较).逻辑运算.短路逻辑运算.三元运算) 三元运算符案例(2只老虎:2个变量比较.三个和尚:3个变量比较) 键 ...

  5. 【笔记】Java的运算符(赋值运算符号、一元运算符、算术运算符、关系运算符、自增与自减运算符、逻辑运算符、括号运算符、位运算符、三元(三目)运算符)、表达式与语句:简洁表达式

    文章目录 一.运算符 1.赋值运算符号 2.一元运算符 3.算术运算符 4.关系运算符 5.自增与自减运算符 6.逻辑运算符 7.括号运算符 8.位运算符 左移位: 右移位: 9.三元(三目)运算符 ...

  6. Java_表达式和运算符(算术运算符、关系运算符、逻辑运算符、位运算符、赋值运算符和三元运算符)

    Java_运算符和表达式 运算符 算术运算符:+.-.*./. %.++.-- 加法(+) 除法(/) 取余(%) 关系运算符:<.>.<=.>=.==.!= 逻辑运算符:&a ...

  7. JAVA之旅(一)——基本常识,JAVA概念,开发工具,关键字/标识符,变量/常量,进制/进制转换,运算符,三元运算

    JAVA之旅(一)--基本常识,JAVA概念,开发工具,关键字/标识符,变量/常量,进制/进制转换,运算符,三元运算 Android老鸟重新学一遍JAVA是什么感觉?枯燥啊,乏味啊,而且归纳写博客,都 ...

  8. JAVA之旅(一)——基本常识,JAVA概念,开发工具,关键字/标识符,变量/常量,进制/进制转换,运算符,三元运算...

    JAVA之旅(一)--基本常识,JAVA概念,开发工具,关键字/标识符,变量/常量,进制/进制转换,运算符,三元运算 Android老鸟重新学一遍JAVA是什么感觉?枯燥啊,乏味啊,而且归纳写博客,都 ...

  9. js“三元表达式” (三元运算符)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/liu_jiachen/article/ ...

最新文章

  1. ELK安装文档及相关优化
  2. jupyter notebook 内核好像挂掉了
  3. html中给%3cb%3e加上颜色,如何使用CSS(jQuery SVG图像替换)更改SVG图像的颜色?
  4. python怎么打印字典_在python中打印字典的原始输入顺序
  5. 谷歌云计算机,google云计算的三大核心技术
  6. 一串最简单的JavaScript代码,在Chrome开发者工具调试器里触发VM8标签的出现
  7. 小白创建网站的曲折之路
  8. UNP Chapter 27 - 客户-服务器程序其他设计方法
  9. JavaScript中本地对象、内置对象和宿主对象
  10. 微信小程序弹出框竖向滚动_微信小程序 解决自定义弹出层滑动时下层页面滚动问题...
  11. cygwin 编译linux内核,【记录】Cygwin下交叉编译Linux内核时用make menuconfig去确认和修改配置...
  12. 《Getting Started with WebRTC》第二章 WebRTC技术介绍
  13. 如何收集常见的前端性能信息
  14. 天翼,有毒?“校园客户端挂马事件”雷锋网独家解析
  15. 软件测试仓库管理信息系统,仓库管理系统测试报告
  16. 51单片机-直流电机
  17. Hydra(弱密码爆破)使用教程
  18. 虚幻4地形怎么增加层_虚幻周报20200512 | 该来的总会来的!
  19. 2018年 第九届 蓝桥杯省赛 C/C++ B 组
  20. 阿里图片合成接口拼接

热门文章

  1. iOS-NSUserDefaults缓存自定义对象
  2. 150330之软件测试
  3. webservice系统学习笔记8-简单的权限校验
  4. Java程序低手之关于泛型(Generic)
  5. [转载] Python 列表(List)
  6. [转载] 两种方法分割python多空格字符串
  7. [转载] Python开发系列课程(16) - 进程和线程
  8. 如何理解FPGA的配置状态字寄存器Status Register
  9. docker 命令2
  10. [转载]程序员的激情其实是一种痛苦