By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,711 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,711 IT Pros & Developers. It's quick & easy.

x*x if x>10

P: n/a
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.
Jul 27 '08 #1
Share this Question
Share on Google+
2 Replies

P: n/a
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
Jul 27 '08 #2

P: n/a
On Jul 27, 10:13*pm, ssecorp <circularf...@gmail.comwrote:
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-valueif <conditionelse <false-value>
it is possible to nest with an else too.
Sure. You can extend the <false-valuewith another ternary operator:

<label= <value1if <condition1else <value2if <condition2else
<value3>
Jul 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.