三元运算符 python_Python三元运算符

三元运算符 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

    原文作者:cunchi4221
    原文地址: https://blog.csdn.net/cunchi4221/article/details/107474122
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞