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

H

Holger

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
 
J

Jussi Salmela

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
 
B

bearophileHUGS

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
 
J

Jussi Salmela

(e-mail address removed) 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
 
P

Peter Otten

Jussi said:
(e-mail address removed) 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
 
J

Jussi Salmela

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
 
P

Peter Otten

Jussi said:
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:
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
 
H

Holger

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.

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
 
C

Carl Banks

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.


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
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

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.
 
G

Gabriel Genellina

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top