468,771 Members | 1,551 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

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

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

Feb 2 '07 #1
11 2001
http://effbot.org/pyfaq/is-there-an-...y-operator.htm

--
EduardoOPadoan (eopadoan->altavix::com)
Bookmarks: http://del.icio.us/edcrypt
Blog: http://edcrypt.blogspot.com
Jabber: edcrypt at jabber dot org
ICQ: 161480283
GTalk: eduardo dot padoan at gmail dot com
MSN: eopadoan at altavix dot com
Feb 2 '07 #2
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
Feb 2 '07 #3
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 '07 #4
be************@lycos.com 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
Feb 3 '07 #5
Jussi Salmela wrote:
be************@lycos.com 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

Feb 3 '07 #6
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
Feb 3 '07 #7
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
Feb 3 '07 #8
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

Feb 11 '07 #9
On Feb 11, 5:16 pm, "Holger" <ish...@gmail.comwrote:
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

Feb 12 '07 #10
On 11 Feb 2007 16:57:07 -0800, Carl Banks <pa************@gmail.comwrote:
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
Feb 12 '07 #11
En Sun, 11 Feb 2007 19:16:49 -0300, Holger <is****@gmail.comescribió:
>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

Feb 12 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by |-|erc | last post: by
4 posts views Thread by renderman | last post: by
10 posts views Thread by Roland | last post: by
16 posts views Thread by Gufus | last post: by
32 posts views Thread by fran7 | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.