Ternary Operator in Python

P

praba kar

Dear All,
I am new to Python. I want to know how to
work with ternary operator in Python. I cannot
find any ternary operator in Python. So Kindly
clear my doubt regarding this



__________________________________
Yahoo! Messenger
Show us what our next emoticon should look like. Join the fun.
http://www.advision.webevents.yahoo.com/emoticontest
 
R

Roy Smith

praba kar said:
Dear All,
I am new to Python. I want to know how to
work with ternary operator in Python. I cannot
find any ternary operator in Python.

You answered your own question; there is no ternary operator in Python.
There was a major debate on this newsgroup a year or so ago on this
subject, and the decision was quite clear that no such feature would be
added.

If you google for "python ternary", you will find a huge amount of material
written on the subject.
 
J

John Roth

praba kar said:
Dear All,
I am new to Python. I want to know how to
work with ternary operator in Python. I cannot
find any ternary operator in Python. So Kindly
clear my doubt regarding this

There isn't one, and there won't be one unless Guido
changes his mind, and that's quite unlikely.

There are a number of workarounds; the most
used one seems to be based on a feature of the
'and' and 'or' operators. I believe Pep 308 has
a summary of the different ways you can do it,
and the advantages and drawbacks of each.

John Roth
 
S

Scott David Daniels

John said:
There isn't one, and there won't be one unless Guido
changes his mind, and that's quite unlikely.

Au contraire, mon frere:

There is a ternary operator in Python (fairly ill-documented)
Its name is "partial polynomial eval." As is traditional in
implementing ternary operations in computer languages, the
name of the operator does not show up anywhere in the code,
so many people don't realize they are using it. Here is an
example of the "ppe" used to evaluate a cubic, using the
ternary operator three times in a single statement:

def cubic(x, a, b, c, d):
return ((a * x + b) * x + c) * x + d

As you may be able to guess, the more common name is "*+",
and it is a nice self-documenting operator (it behaves just
like the primitives). Originally the DEC Vax provided this
as a "vectorized" opcode, which is where Python got the
idea. You probably don't see it since you aren't doing much
engineering work.

--Scott David Daniels
(e-mail address removed)

-- No truth has been harmed by this April Fool's post. :)
 
R

Ron_Adam

Dear All,
I am new to Python. I want to know how to
work with ternary operator in Python. I cannot
find any ternary operator in Python. So Kindly
clear my doubt regarding this



__________________________________
Yahoo! Messenger
Show us what our next emoticon should look like. Join the fun.
http://www.advision.webevents.yahoo.com/emoticontest


I've used boolean opperations to do it.

result = (v == value) * first + (v != value) * second

Same as:

if v == value: result = first else: result = second


Ron
 
E

Erik Max Francis

Ron_Adam said:
I've used boolean opperations to do it.

result = (v == value) * first + (v != value) * second

Same as:

if v == value: result = first else: result = second

No, it isn't, because it isn't short circuiting. If first or second had
side effects, then the two would not be equivalent.
 

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

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top