473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Ternary Operator in Python

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
Jul 18 '05 #1
6 3788
praba kar wrote:
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.


http://www.python.org/peps/pep-0308.html
Jul 18 '05 #2
praba kar <pr*********@yahoo.co.in> wrote:
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.
Jul 18 '05 #3

"praba kar" <pr*********@yahoo.co.in> wrote in message
news:ma**************************************@pyth on.org...
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
__________________________________
Yahoo! Messenger
Show us what our next emoticon should look like. Join the fun.
http://www.advision.webevents.yahoo.com/emoticontest


Jul 18 '05 #4
John Roth wrote:

"praba kar" <pr*********@yahoo.co.in> wrote in message
news:ma**************************************@pyth on.org...
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.


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
Sc***********@Acm.Org

-- No truth has been harmed by this April Fool's post. :-)
Jul 18 '05 #5
On Fri, 1 Apr 2005 08:24:42 +0100 (BST), praba kar
<pr*********@yahoo.co.in> wrote:
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

Jul 18 '05 #6
Ron_Adam wrote:
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.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
If the sun comes up / And you're not home / I'll be strong
-- India Arie
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: zipher | last post by:
It seems the debate over PEP 308 (if-then-else expression) occurred prior to the arrival of generator expressions. Mightn't this new latter syntax be the ticket to a "one obvious way" to write a...
2
by: praba kar | last post by:
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 With regards,...
6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
3
by: Ben Wilson | last post by:
I read somewhere else that Python was getting a ternary operator (e.g. x = (true/false) ? y : z). I read the PEP about it and that the PEP had been approved this past Fall. Has this been released...
15
by: Arthur Dent | last post by:
Hi all, im just curious if anyone knows..... With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck away in a corner somewhere? By ternary, i mean something like C's a?b:c...
5
by: PerlPhi | last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...
0
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...

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.