Is there a python equivalent of this trick in C?
Logic_Test ? True_Result : False_Result
Example:
printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" ); 15 1674
On 9 Aug 2004 23:55:08 -0700, Dan <da**********@gmail.com> wrote: Is there a python equivalent of this trick in C?
Logic_Test ? True_Result : False_Result
Example: printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] )
But I don't know if int(False)==0 and int(True)==1 are actually
guaranteed. And I wouldn't use it since it's ugly (IMHO).
You don't get the "only evaluate one" goodness of ?:
--
Sam Holden
Dan wrote: Is there a python equivalent of this trick in C?
Logic_Test ? True_Result : False_Result
Logic_Test and True_Result or False_Result
would be a Python counterpart.
Example: printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
So the exmaple is converted to:
print "you have %d %s"%(num_eggs, (num_eggs > 1) and "eggs" or "egg")
But be warned that if True_Result is evaluated as False(e.g. True_Result
is an empty string), this doesn't work.
If you don't know what object is considered False in Python, check the
following document.
* 2.3.1 Truth Value Testing http://docs.python.org/lib/truth.html
--
George
Dan wrote: Is there a python equivalent of this trick in C?
Logic_Test ? True_Result : False_Result
The long answer to your question is at http://www.python.org/peps/pep-0308.html
(in addition to the "hack" Sam posted :-)
Roger
Dan wrote: Is there a python equivalent of this trick in C?
Logic_Test ? True_Result : False_Result
Example: printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
Logic_Test and True_Result or False_Result
Example:
print (num_eggs > 1) and 'eggs" or "egg"
Thomas
Thomas Krüger wrote: Logic_Test and True_Result or False_Result
Example: print (num_eggs > 1) and 'eggs" or "egg"
Be very careful that the "True_Result" in your formulation actually is
itself a Python true value, or this won't work:
print 'egg' + (numEggs == 1 and '' or 's')
will not work as expected.
--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Melancholy men, of all others, are the most witty.
-- Aristotle
Sam Holden wrote: But I don't know if int(False)==0 and int(True)==1 are actually guaranteed. And I wouldn't use it since it's ugly (IMHO).
From the language ref at http://docs.python.org/ref/types.html#l2h-37
Booleans
These represent the truth values False and True. The two objects
representing the values False and True are the only Boolean objects.
The Boolean type is a subtype of plain integers, and Boolean values
behave like the values 0 and 1, respectively, in almost all contexts,
the exception being that when converted to a string, the strings
"False" or "True" are returned, respectively.
So yes, it's fully guaranteed.
"Sam Holden" <sh*****@flexal.cs.usyd.edu.au> wrote in message
news:slrnchgsml.8e4.sh*****@flexal.cs.usyd.edu.au. .. On 9 Aug 2004 23:55:08 -0700, Dan <da**********@gmail.com> wrote: Is there a python equivalent of this trick in C?
Logic_Test ? True_Result : False_Result
Example: printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] )
I would choose "!=" instead of ">" as the comparison operator. I think the
accepted vernacular is:
you have -2 eggs
you have -1 eggs
you have 0 eggs
you have 1 egg
you have 2 eggs
you have 3 eggs
you have 0.5 eggs
....
-- Paul
>>>Example: printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] )
I would choose "!=" instead of ">" as the comparison operator. I think the accepted vernacular is:
you have -2 eggs you have -1 eggs you have 0 eggs you have 1 egg you have 2 eggs you have 3 eggs you have 0.5 eggs
No offense intended, but the negative and float cases don't make any
sense for me on this context. You can't have -2 eggs or 0.5 eggs. The
last case won't occure as well because the output is being parsed to
int, so, you will get "you have 0 egg", which is again false. This error
happens with both solutions (with > and with !=).
Regards,
Josef
Josef Meile wrote: Example: printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] ) I would choose "!=" instead of ">" as the comparison operator. I think the accepted vernacular is:
you have -2 eggs you have -1 eggs you have 0 eggs you have 1 egg you have 2 eggs you have 3 eggs you have 0.5 eggs
No offense intended, but the negative and float cases don't make any sense for me on this context. You can't have -2 eggs or 0.5 eggs. The last case won't occure as well because the output is being parsed to int, so, you will get "you have 0 egg", which is again false. This error happens with both solutions (with > and with !=).
Correction: The error happens only when using the ">" operator. With the
"!=" doesn't happen.
"Josef Meile" <jm****@hotmail.com> wrote in message
news:41********@pfaff2.ethz.ch... Example: printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] )
I would choose "!=" instead of ">" as the comparison operator. I think
the accepted vernacular is:
you have -2 eggs you have -1 eggs you have 0 eggs you have 1 egg you have 2 eggs you have 3 eggs you have 0.5 eggs
No offense intended, but the negative and float cases don't make any sense for me on this context. You can't have -2 eggs or 0.5 eggs. The last case won't occure as well because the output is being parsed to int, so, you will get "you have 0 egg", which is again false. This error happens with both solutions (with > and with !=).
Regards, Josef
No offense taken! :)
The OP was looking for a general solution to <condition> ? <if-true-action>
: <else-action>, then gave us the "you have n egg(s)" example.
In the general case, the number for comparison isn't always an integer
quantity such as eggs. It could be "dollar(s)"/"euro(s)"/"Swiss
franc(s)"/"yen" (hmm, I guess "yen" isn't a problem...), or "ton(s) of
salami", or "pound(s) of cement", or "degree(s) Celsius", or... Anyway,
even though eggs are usually counted from 1 to n in integer steps, other
quantities can easily be negative and/or continuous. Still the singular -
in English, anyway - is usually used *only* when the quantity is 1.
Fractional and zero amounts, even though less than 1, still most naturally
use the plural form.
You have 0.5 dollars
You have gained 1 pound
You increased temperature by 0 degrees
You have -2 dollars (that is, you owe 2 dollars)
My point (which I guess didn't come across too well) was that this is a
typical coding and testing error, in which only positive integer values > 0
are assumed, because we often mentally equate "plural" with "more than 1".
But whether you are working in an integer, real, positive-only, or all
numbers context, testing with n != 1 should determine whether singular noun
should be used.
-- Paul
Erik Max Francis <ma*@alcyone.com> wrote in message news:<41***************@alcyone.com>... Thomas Krüger wrote:
Logic_Test and True_Result or False_Result
Example: print (num_eggs > 1) and 'eggs" or "egg"
Be very careful that the "True_Result" in your formulation actually is itself a Python true value, or this won't work:
print 'egg' + (numEggs == 1 and '' or 's')
will not work as expected.
For which reason the form (test and [true_result] or
[false_result])[0] is sometimes advocated. This starts to look a
little too cluttered for my liking, so I tend to use it only when the
'true_result' is a variable and might be instantiated to a negative
value.
Paul McGuire wrote: No offense taken! :)
The OP was looking for a general solution to <condition> ? <if-true-action> : <else-action>, then gave us the "you have n egg(s)" example.
So, I guess it is like the "foo" keyword.
In the general case, the number for comparison isn't always an integer quantity such as eggs. It could be "dollar(s)"/"euro(s)"/"Swiss franc(s)"/"yen" (hmm, I guess "yen" isn't a problem...), or "ton(s) of salami", or "pound(s) of cement", or "degree(s) Celsius", or... Anyway, even though eggs are usually counted from 1 to n in integer steps, other quantities can easily be negative and/or continuous. Still the singular - in English, anyway - is usually used *only* when the quantity is 1. Fractional and zero amounts, even though less than 1, still most naturally use the plural form.
You have 0.5 dollars You have gained 1 pound You increased temperature by 0 degrees You have -2 dollars (that is, you owe 2 dollars)
Good point ;-)
My point (which I guess didn't come across too well) was that this is a typical coding and testing error, in which only positive integer values > 0 are assumed, because we often mentally equate "plural" with "more than 1". But whether you are working in an integer, real, positive-only, or all numbers context, testing with n != 1 should determine whether singular noun should be used.
I like your solution and I think it even considers when n=0, while the
other example fails and write "You have 0 egg"
On Tue, 10 Aug 2004 23:23:07 GMT,
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote: You have 0.5 dollars You have gained 1 pound You increased temperature by 0 degrees You have -2 dollars (that is, you owe 2 dollars)
Don't forget this one: You have -1 egg.
Regards,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Never play leapfrog with a unicorn.
Dan wrote: Is there a python equivalent of this trick in C?
Logic_Test ? True_Result : False_Result
There's something close:
Logic_Test and True_Result or False_Result
print 'you have %i egg%s' % (num_eggs, (num_eggs != 1) and 's' or '')
The main problem with this is that False_Result will get executed if
Logic_Test is false *or* True_Result evaluates to false. Witness the
following broken variation of the above:
print 'you have %i egg%s' % (num_eggs, (num_eggs == 1) and '' or 's')
So it's not perfect, but it is workable. And as a previous post
indicated, this situation is not likely to change. :)
-- Mark
Dan wrote: Is there a python equivalent of this trick in C?
Logic_Test ? True_Result : False_Result
Example: printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" ); def numerus(n, sg, pl=None):
.... if n == 1:
.... return article(sg)
.... elif n == 0:
.... return "no " + plural(sg, pl)
.... else:
.... return "%d %s" % (n, plural(sg, pl))
.... def article(sg):
.... if sg[:1].lower() in "aeiou": # uniforms are off-limit
.... return "an " + sg
.... else:
.... return "a " + sg
.... def plural(sg, pl):
.... if pl is None:
.... return sg + "s"
.... else:
.... return pl
.... numerus(1, "egg")
'an egg' numerus(2, "egg")
'2 eggs' numerus(0, "egg")
'no eggs' numerus(1, "solution")
'a solution'
:-)
Refine as needed.
Peter
PS: The trick is to not use a trick. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Raymond Hettinger |
last post by:
Here are four more mini-mysteries for your amusement
and edification.
In this episode, the program output is not shown.
Your goal is to predict the output and, if anything
mysterious occurs,...
|
by: Lad |
last post by:
Is anyone capable of providing Python advantages over PHP if there are
any?
Cheers,
L.
|
by: Joseph Garvin |
last post by:
As someone who learned C first, when I came to Python everytime I read
about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(),
getattr/setattr, the % operator, all of this was very...
|
by: Jan Danielsson |
last post by:
Hello all,
How do I make a python script actually a _python_ in unix:ish
environments?
I know about adding:
#!/bin/sh
..as the first row in a shell script, but when I installed python on...
|
by: ajikoe |
last post by:
Hi,
I tried to follow the example in swig homepage.
I found error which I don't understand.
I use bcc32, I already include directory where my python.h exist in
bcc32.cfg.
/* File : example.c...
|
by: Xah Lee |
last post by:
Python, Lambda, and Guido van Rossum
Xah Lee, 2006-05-05
In this post, i'd like to deconstruct one of Guido's recent blog about
lambda in Python.
In Guido's blog written in 2006-02-10 at...
|
by: mistral |
last post by:
Need compile python code, source is in html and starts with parameters:
#!/bin/sh -
"exec" "python" "-O" "$0" "$@"
I have installed ActivePython for windows.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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,...
| |