473,326 Members | 2,148 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,326 software developers and data experts.

trinary operator - if then else

What is Python's version for the trinary if..then..else operator?

I want a one-liner such as
a?b:c
for the if..then..else control structure
if a
then b
else c

Does Python 2.4 support it?

Apr 25 '07 #1
4 4343
Alchemist wrote:
What is Python's version for the trinary if..then..else operator?
true_value if condition else false_value
Does Python 2.4 support it?
No, it requires 2.5 or later.

Peter
Apr 25 '07 #2

On Apr 25, 2007, at 1:58 AM, Alchemist wrote:
What is Python's version for the trinary if..then..else operator?

I want a one-liner such as
a?b:c
for the if..then..else control structure
if a
then b
else c

Does Python 2.4 support it?
Not precisely, but you can *usually* get away with:

a and b or c

Apr 25 '07 #3
Michael >>
>Does Python 2.4 support it?

Not precisely, but you can *usually* get away with:

a and b or c
This is really bad advice, as long as you don't explain why it "usually"
works (and often enough not). This for example won't work:
>>False or '' and 0
''

The reason is that the if-value has to be bool(v) == True, otherwise the
short-circuiting of the and will prevent the 0 from being returned.

So in general, _don't_ use this!

Diez
Apr 25 '07 #4
Diez B. Roggisch <de***@nospam.web.dewrote:
Michael >>
Does Python 2.4 support it?
Not precisely, but you can *usually* get away with:

a and b or c

This is really bad advice, as long as you don't explain why it "usually"
works (and often enough not). This for example won't work:
>False or '' and 0
''
You can use this if you want it to be bullet proof

(a and [b] or [c])[0]

Not exactly elegant though!

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Apr 25 '07 #5

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

Similar topics

1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
3
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h...
2
by: ryan.fairchild | last post by:
I have a problem I am trying to create a MyInt class to hanlde very large ints. Its for a class, therefore I can only do what the teach tells me. I want to be able to overload the insertion...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
12
by: y-man | last post by:
Hi, I am creating a child class of the vector, which contains a couple of functions to make the work with carthesian coordinate vectors easier. To make things work more easily, I would like to...
3
by: namrata1980 | last post by:
Hello please explain the following:- Say if int i,sign; double atof(char s); has been declared and then there is statement sign=(s=='-' ) ? -1:1;
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
4
by: fabian.lim | last post by:
Hi All, Im a newbie to C++, I am trying to customize the vector template STL to a template Class. My code is shown below. Im confused about something and maybe somebody here might be able to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
1
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: 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.