x*x if x>10

alex23 wuwei23 at gmail.com
Sun Jul 27 08:24:36 EDT 2008


On Jul 27, 10:13 pm, ssecorp <circularf... at gmail.com> wrote:
> I have seen somewhere that you can write something like:
>
> x*x if x>10
>
> but exactly that doesn't work and I can't get any variation to work.

It's called a ternary operator. The format is:

<label> = <true-value> if <condition> else <false-value>

> it is possible to nest with an else too.

Sure. You can extend the <false-value> with another ternary operator:

<label> = <value1> if <condition1> else <value2> if <condition2> else
<value3>



More information about the Python-list mailing list