x*x if x>10

Diez B. Roggisch deets at nospam.web.de
Sun Jul 27 14:17:00 CEST 2008


ssecorp schrieb:
> 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 is possible to nest with an else too.
> 
> 
> how do you write it?
> 
> 
> and also, is it idiomatic? doesn't seem to add functionality, just
> another way of doing the same thing which is quite unpythonic but I
> remember reading it was added because it helped simplify the
> expression of a certain type of operation.


It's a ternary operator as found in e.g. C or Java like this;


foo = <condition> ? <true-value> : <false-value>;

And it's become available in python2.5, anything below that version will 
throw an error.

Diez



More information about the Python-list mailing list