473,503 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2215
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2431
by: |-|erc | last post by:
<?php // Get the names and values for vars sent by index.lib.php3 if (isset($HTTP_GET_VARS)) { while(list($name,$value) = each($HTTP_GET_VARS)) { $$name = $value; }; };
4
2016
by: renderman | last post by:
Hi, I looked through a computergenerated code because I had to edit something manually. I found this line: <xsl:when test="''='FALSE'"> I don't get what this means.
17
7333
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
13
3266
by: Jalal | last post by:
I am trying to use numeric_limits<double>::min() in an MFC application in Microsoft Visual C++.NET 2003. I am having some difficulties here. The following are the parts of a simple program I wrote...
10
16530
by: Roland | last post by:
Hello, the example code is the following(the number in parentheses at the beginning are just for reference)(The complete HTML file is at the end of this article): (1)window.location =...
16
4420
by: Gufus | last post by:
Hi Group... <a name="#top"></a> ----del---- ----del---- <img SRC="images/top.gif" USEMAP="#top" BORDER=0> <map NAME="top">
3
3092
by: André | last post by:
Hi, I put that question already, but it's still not very clear to me, so ... Assume following option in web.config= debug="false" but in one aspx page (test.aspx) <%@ debug="true" ..%>
2
13669
by: riceyeh | last post by:
Hi, What does <xsl:if test="not($values)"mean? What I do not understand is $values? Here, means array? And . = $value means current node is equal to the variable value? So the total meaning is...
32
3840
by: fran7 | last post by:
Hi, Trying to solve a problem with no knowledge of asp. I have a dropdown list populated from my database with card decription. The output is names like, abstract,illustration etc. I am sending...
17
5795
by: malathib | last post by:
Hi, I have used a rich text editor in my application. I got it from one of the site.all are JS files. My problem is, i want to call a javascript function from the iframe. Can anybody help...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.