condition ? expression1 : expression2
, where expression1
is taken if condition
is true, and expression2
is taken if condition
is false.a ? b : c
can be read as “if the condition a
is true, then b
is returned; otherwise, c
is returned.”// use if-else
if (a > b) {
result = x;
} else {
result = y;
}
// use the ternary operator
result = a > b ? x : y;
<condition> ? <expression1> : <expression2>
<condition> then <expression1> else <expression2>
(if <condition>: <expression1> else: <expression2>)
<condition> and <expression1> else <expression2>
<expression1> if <condition> else <expression2>
cond(<condition>, <expression1>, <expression2>)
(if <condition>: <expression1> else: <expression2>)
, which is a flattened version of the conventional if-else syntax that is easy to understand. However, the downside is that it requires parentheses, which can be confused with generator expressions, and requires special treatment of the colon by the interpreter.<expression1> if <condition> else <expression2>
, which was the recommended solution in the earliest version of PEP-308. However, some people find the style of not placing the condition first uncomfortable, and when “expression1” is long, it is easy to overlook its condition.<condition> and <expression1> or <expression2>
syntax to implement conditional selection. However, this syntax behaves differently in Python than in some other languages, and if used improperly, it can result in bugs!a = True and True or "Python cat"
b = True and False or "Python cat"
<condition> and <expression1> or <expression2>
, if the condition is false, expression2 is evaluated and returned directly. If the condition is true, expression1 is evaluated first. If it is also true, expression2 will not be evaluated further. If expression1 is false, expression2 will be evaluated.X if C else Y
. As a result, PEP-308 was reopened and updated, and it was soon implemented in the 2.5 version the following year.X if C else Y
is very easy to understand and highly readable. It continues the style of “explicit is better than implicit” by using the intuitive and conversational “if-else” instead of introducing potentially confusing punctuation, just like how Python chooses the words “and” and “or” instead of the symbols ”&&” and ”||“.(if <condition>: <expression1> else: <expression2>)
.X if C else Y
, Guido examined all the “and-or” combinations in the standard library and found that those written as C and X or Y
could be replaced by X if C else Y
. The situation in the standard library proved that this new syntax is feasible.X if C else Y
design was actually to eliminate the pitfalls of the “and-or” syntax. This design is concise and easy to read.?:
operator?“.?:
operator and instead recommends using the native “if-else” syntax. The explanation in the documentation is brief, with only one sentence:The reason
?:
is absent from Go is that the language’s designers had seen the operation used too often to create overly complex expressions. Theif-else
form, although longer, is unquestionably clearer. A language needs only one conditional control flow construct.
if
syntax is not a “statement” like in other languages, but an “expression”, which means that you can directly assign the if
expression to a variable:// Gets 5 if the condition is true, otherwise 6
let number = if condition { 5 } else { 6 };
let x = 42;
let result = if x > 50 {
println!("x is greater than 50");
x * 2 // This is an expression that will assign its returned value to the variable 'result'
} else {
println!("x is less than or equal to 50");
x / 2
};
if
is an expression instead of a statement, such as Kotlin, Scala, F#, and Swift. They theoretically do not need to use the ternary operator. (As an aside, Swift is an exception, and it also has a ternary operator. Kotlin has the “?: ” operator, note that the two symbols are connected together. val result = a ?: b
means: if a
is not null
, assign it to result
; otherwise, assign b
to result
.)