473,320 Members | 1,832 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,320 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 3785
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.