compound statement from C "<test>?<true-val>:<false-val>"

Discussion in 'Python' started by Holger, Feb 2, 2007.

  1. Holger

    Holger Guest

    Hi

    I have not been able to figure out how to do compound statement from C
    - "<test>?<true-val>:<false-val>"

    But something similar must exist...?!

    I would like to do the equivalent if python of the C line:
    printf("I saw %d car%s\n", n, n != 1 ? "s" : "")

    Please help

    /Holger
    Holger, Feb 2, 2007
    #1
    1. Advertising

  2. Eduardo \EdCrypt\ O. Padoan, Feb 2, 2007
    #2
    1. Advertising

  3. Holger kirjoitti:
    >
    > I would like to do the equivalent if python of the C line:
    > printf("I saw %d car%s\n", n, n != 1 ? "s" : "")
    >
    > Please help
    >
    > /Holger
    >


    In this particular case you don't need the ternary operator:

    print "I saw %d car%s\n" % (n, ("", "s")[n != 1])


    Cheers,
    Jussi
    Jussi Salmela, Feb 2, 2007
    #3
  4. Holger

    Guest

    Jussi Salmela:
    > In this particular case you don't need the ternary operator:
    > print "I saw %d car%s\n" % (n, ("", "s")[n != 1])


    The last newline is probably unnecessary. This seems be a bit more
    readable:
    print "I saw", n, "car" + ("", "s")[n != 1]

    With Python 2.5 this looks better:
    print "I saw", n, "car" + ("" if n == 1 else "s")

    Or the vesion I like better:
    print "I saw", n, ("car" if n == 1 else "cars")

    Those () aren't necessary, but they help improve readability, and
    avoid problems with operator precedence too. That if has a quite low
    precedence.

    Bye,
    bearophile
    , Feb 2, 2007
    #4
  5. kirjoitti:
    > Jussi Salmela:
    >> In this particular case you don't need the ternary operator:
    >> print "I saw %d car%s\n" % (n, ("", "s")[n != 1])

    >
    > The last newline is probably unnecessary. This seems be a bit more
    > readable:
    > print "I saw", n, "car" + ("", "s")[n != 1]
    >
    > With Python 2.5 this looks better:
    > print "I saw", n, "car" + ("" if n == 1 else "s")
    >
    > Or the vesion I like better:
    > print "I saw", n, ("car" if n == 1 else "cars")
    >
    > Those () aren't necessary, but they help improve readability, and
    > avoid problems with operator precedence too. That if has a quite low
    > precedence.
    >
    > Bye,
    > bearophile
    >

    This is getting weird but here's 2 more in the spirit of
    "who needs the ternary operator - I don't!". And I'm starting to
    wonder what the 'obvious way' (as in 'Zen of Python') to write
    this would be.

    print "I saw %d car%s" % (n, {1:''}.get(n==1, 's'))

    print "I saw %d car%s" % (n, 's'*(n!=1))

    Cheers,
    Jussi
    Jussi Salmela, Feb 3, 2007
    #5
  6. Holger

    Peter Otten Guest

    Jussi Salmela wrote:

    > kirjoitti:
    >> Jussi Salmela:
    >>> In this particular case you don't need the ternary operator:
    >>> print "I saw %d car%s\n" % (n, ("", "s")[n != 1])

    >>
    >> The last newline is probably unnecessary. This seems be a bit more
    >> readable:
    >> print "I saw", n, "car" + ("", "s")[n != 1]
    >>
    >> With Python 2.5 this looks better:
    >> print "I saw", n, "car" + ("" if n == 1 else "s")
    >>
    >> Or the vesion I like better:
    >> print "I saw", n, ("car" if n == 1 else "cars")
    >>
    >> Those () aren't necessary, but they help improve readability, and
    >> avoid problems with operator precedence too. That if has a quite low
    >> precedence.
    >>
    >> Bye,
    >> bearophile
    >>

    > This is getting weird but here's 2 more in the spirit of
    > "who needs the ternary operator - I don't!". And I'm starting to
    > wonder what the 'obvious way' (as in 'Zen of Python') to write
    > this would be.
    >
    > print "I saw %d car%s" % (n, {1:''}.get(n==1, 's'))
    >
    > print "I saw %d car%s" % (n, 's'*(n!=1))


    Isn't that obvious? Don't do it in one line:

    if n == 1:
    print "I saw a car"
    else:
    print "I saw %d cars" % n

    I guess that most of us will have read, understood, and verified (are there
    any errors or cases that should be covered but aren't) those four lines
    faster than any of the "smart" constructs, including the official 2.5
    ternary operator. Now modify all proposed versions to print

    I didn't see any cars
    I saw 7 cars missing

    for n=0 and n=-7, respectively, and you will see 1 light :)

    Peter
    Peter Otten, Feb 3, 2007
    #6
  7. Peter Otten kirjoitti:
    > Isn't that obvious? Don't do it in one line:
    >
    > if n == 1:
    > print "I saw a car"
    > else:
    > print "I saw %d cars" % n
    >
    > I guess that most of us will have read, understood, and verified (are there
    > any errors or cases that should be covered but aren't) those four lines
    > faster than any of the "smart" constructs, including the official 2.5
    > ternary operator. Now modify all proposed versions to print
    >
    > I didn't see any cars
    > I saw 7 cars missing
    >
    > for n=0 and n=-7, respectively, and you will see 1 light :)
    >
    > Peter
    >


    It's naturally clear that a combination of if-elifs-else is more
    adaptable to different situations, but the OP's question was:

    I would like to do the equivalent if python of the C line:
    printf("I saw %d car%s\n", n, n != 1 ? "s" : "")

    In this question I thought I recognized the familiar
    tool=hammer==>problem:nail pattern of thought and tried to show
    that in addition to the ternary operator Python has other ways of
    resolving that particular problem of his.

    I'm certainly not an advocate of one-liners because at their extreme
    they easily result in write-only solutions.

    Cheers,
    Jussi
    Jussi Salmela, Feb 3, 2007
    #7
  8. Holger

    Peter Otten Guest

    Jussi Salmela wrote:

    > It's naturally clear that a combination of if-elifs-else is more
    > adaptable to different situations, but the OP's question was:
    >
    > I would like to do the equivalent if python of the C line:
    > printf("I saw %d car%s\n", n, n != 1 ? "s" : "")


    And my answer, triggered by your intermission

    > And I'm starting to wonder what the 'obvious way' (as in 'Zen of Python')
    > to write this would be.


    was that in Python you would achieve the best results with if ... else
    instead:

    >> if n == 1:
    >> print "I saw a car"
    >> else:
    >> print "I saw %d cars" % n


    > In this question I thought I recognized the familiar
    > tool=hammer==>problem:nail pattern of thought and tried to show
    > that in addition to the ternary operator Python has other ways of
    > resolving that particular problem of his.


    It seems we operated on different levels of abstraction,

    You: hammer=ternary operator
    Me: hammer=oneliner

    > I'm certainly not an advocate of one-liners because at their extreme
    > they easily result in write-only solutions.


    D'accord. Did I mention that, as a "for fun" approach, "s" * (n != 1) is
    quite clever :)

    Peter
    Peter Otten, Feb 3, 2007
    #8
  9. Holger

    Holger Guest

    Thanks all for good input.
    It seems like there's no the-python-way for this one.

    Currently I'm forced to use cygwin - and python in cygwin is still not
    2.5 so I can't use the new inline if-else ternary operator.

    > >> if n == 1:
    > >> print "I saw a car"
    > >> else:
    > >> print "I saw %d cars" % n


    Personally I don't like the if-else approach because of the don't-
    repeat-yourself philosophy

    > D'accord. Did I mention that, as a "for fun" approach, "s" * (n != 1) is
    > quite clever :)
    >
    > Peter


    I like this one :)

    > print "I saw %d car%s\n" % (n, ("", "s")[n != 1])


    And this one.

    /Holger
    Holger, Feb 11, 2007
    #9
  10. Holger

    Carl Banks Guest

    On Feb 11, 5:16 pm, "Holger" <> wrote:
    > Thanks all for good input.
    > It seems like there's no the-python-way for this one.
    >
    > Currently I'm forced to use cygwin - and python in cygwin is still not
    > 2.5 so I can't use the new inline if-else ternary operator.
    >
    > > >> if n == 1:
    > > >> print "I saw a car"
    > > >> else:
    > > >> print "I saw %d cars" % n

    >
    > Personally I don't like the if-else approach because of the don't-
    > repeat-yourself philosophy


    You shouldn't be worried a repeating few characters from a short,
    simple print statement. It's not a mortal sin.

    You don't need any ternary operator to avoid repetition, anyways. You
    could factor the common parts out like this:

    if n == 1:
    what = "a car"
    else:
    what = "%d cars" % n
    print "I saw %s" % what

    but what's the point? It's just a few repeated characters two lines
    apart. Peter's version is the most easily read version here,
    including the one using the official ternary operator.


    Carl Banks
    Carl Banks, Feb 12, 2007
    #10
  11. On 11 Feb 2007 16:57:07 -0800, Carl Banks <> wrote:
    > You don't need any ternary operator to avoid repetition, anyways. You
    > could factor the common parts out like this:
    >
    > if n == 1:
    > what = "a car"
    > else:
    > what = "%d cars" % n
    > print "I saw %s" % what


    Or even better (IMHO):

    what = "%d cars" % n
    if n == 1:
    what = "a car"
    print "I saw %s" % what

    One less line and just as readable.

    > but what's the point? It's just a few repeated characters two lines
    > apart. Peter's version is the most easily read version here,
    > including the one using the official ternary operator.


    Agreed.

    --
    mvh Björn
    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, Feb 12, 2007
    #11
  12. En Sun, 11 Feb 2007 19:16:49 -0300, Holger <> escribió:

    >> >> if n == 1:
    >> >> print "I saw a car"
    >> >> else:
    >> >> print "I saw %d cars" % n

    >
    > Personally I don't like the if-else approach because of the don't-
    > repeat-yourself philosophy
    >
    >> D'accord. Did I mention that, as a "for fun" approach, "s" * (n != 1) is
    >> quite clever :)

    > I like this one :)
    >
    >> print "I saw %d car%s\n" % (n, ("", "s")[n != 1])

    > And this one.


    I presume all of this is only used as an example on using expressions. In
    any application with any chances of being i18n, the only viable way is the
    first one. Doing algebra on phrases is a no-no.

    --
    Gabriel Genellina
    Gabriel Genellina, Feb 12, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWlrZUw=?=
    Replies:
    0
    Views:
    383
    =?Utf-8?B?TWlrZUw=?=
    Nov 19, 2004
  2. Damir Mikoc

    CMR/CMP and Compound Primary Key

    Damir Mikoc, Jul 3, 2003, in forum: Java
    Replies:
    1
    Views:
    558
    Christopher Blunck
    Jul 4, 2003
  3. Apc
    Replies:
    1
    Views:
    847
  4. Greg N.
    Replies:
    20
    Views:
    1,018
    Greg N.
    Feb 15, 2006
  5. Replies:
    9
    Views:
    265
    Ben Finney
    Aug 29, 2007
Loading...

Share This Page