x*x if x>10

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ssecorp

    x*x if x>10

    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.
  • Diez B. Roggisch

    #2
    Re: x*x if x>10

    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

    Comment

    • alex23

      #3
      Re: x*x if x&gt;10

      On Jul 27, 10:13 pm, ssecorp <circularf...@g mail.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 <conditionels e <false-value>
      it is possible to nest with an else too.
      Sure. You can extend the <false-valuewith another ternary operator:

      <label= <value1if <condition1el se <value2if <condition2el se
      <value3>

      Comment

      Working...