473,786 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conditional Expressions in Python 2.4

A.M
Hi,

I am using Python 2.4. I read the PEP 308 at:

http://www.python.org/dev/peps/pep-0308/

I tried the statement:

a= "Yes" if 1==1 else "No"

but the interpreter doesn't accept it.

Do we have the conditional expressions in Python 2.4?

Thank you,

Alan
Jun 1 '06 #1
5 1699
A.M wrote:
Do we have the conditional expressions in Python 2.4?


No.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jun 1 '06 #2
A.M wrote:
Hi,

I am using Python 2.4. I read the PEP 308 at:

http://www.python.org/dev/peps/pep-0308/

I tried the statement:

a= "Yes" if 1==1 else "No"

but the interpreter doesn't accept it.

Do we have the conditional expressions in Python 2.4?


No, AFAIK they'll be in for 2.5

In the meanwhile, there are (sometime trickyà ways to get the same result:

a = 1 == 1 and "Yes" or "No"
a = ("No", "Yes")[1 == 1]
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Jun 2 '06 #3
A.M
a = 1 == 1 and "Yes" or "No"
a = ("No", "Yes")[1 == 1]


Smart! Thanks alot.
Jun 2 '06 #4
A.M wrote:
Do we have the conditional expressions in Python 2.4?
bruno at modulix wrote: No, AFAIK they'll be in for 2.5
Yep:

Python 2.5a2 (trunk:46491M, May 27 2006, 14:43:55) [MSC v.1310 32 bit
(Intel)] on win32
"Yes" if 1 == 1 else "No" 'Yes'
In the meanwhile, there are (sometime trickyà ways to get the same result:

a = 1 == 1 and "Yes" or "No"
a = ("No", "Yes")[1 == 1]

And just to give some examples where the conditional expression will
show a difference::
True and 0 or [] [] 0 if True else [] 0
def f(): .... print "don't evaluate me"
.... return 'f'
.... def g(): .... return 'g'
.... (f(), g())[True] don't evaluate me
'g' g() if True else f()

'g'
STeVe
Jun 2 '06 #5
Steven Bethard a écrit :
A.M wrote:
> Do we have the conditional expressions in Python 2.4?
bruno at modulix wrote:

(snip)
In the meanwhile, there are (sometime trickyà ways to get the same
result:

a = 1 == 1 and "Yes" or "No"
a = ("No", "Yes")[1 == 1]
And just to give some examples where the conditional expression will
show a difference::
>>> True and 0 or [] []


Yes, this is one of the tricky part !-)
<op>
Always make sure the second term doesn't eval to False.
</op>
>>> def f(): ... print "don't evaluate me"
... return 'f'
... >>> def g(): ... return 'g'
... >>> (f(), g())[True]


Why on earth are you calling the callables *before* testing ?
Should be:

(f, g)[True]()

of course.
Jun 2 '06 #6

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

Similar topics

11
1518
by: Elaine Jackson | last post by:
If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True, then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j without evaluating any other B_i. This is such a useful mode of expression that I would like to be able to use something similar even when there is an i with bool(B_i)==False. The only thing I can think of by myself is ( (A_1 and ) or ... or (A_n and ) ), and I can't be satisfied with that for...
8
2290
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far from being a "language internist" (on Python or anything else) so go easy on me if this is stupid - it just seemed quite elegant to me as a relative newbie in town :-) I also havent got a clue whether this would be easy or even possible
26
1786
by: Ney André de Mello Zunino | last post by:
Hello. I have noticed, in a lot of C and C++ code, that many programmers seem to prefer putting the test values first in conditional expressions. I.e., they would rather write this: if (-1 == foobar()) than this:
62
3316
by: Reinhold Birkenfeld | last post by:
Hi, after Guido's pronouncement yesterday, in one of the next versions of Python there will be a conditional expression with the following syntax: X if C else Y which is the same as today's (Y, X)
6
12142
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null : DateTime.Now; } When the condition is false, I want to return null. If true, I want to return the current date/time.
8
5114
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function constructors (these three I knew about), but also conditional function definitions, as described in http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions ]. An example: function fun() {
11
5886
by: volcs0 | last post by:
I've done this in Scheme, but I'm not sure I can in Python. I want the equivalent of this: if a == "yes": answer = "go ahead" else: answer = "stop" in this more compact form:
4
1536
by: Colin J. Williams | last post by:
It would be helpful if the rules of the game were spelled out more clearly. The conditional expression is defined as X if C else Y. We don't know the precedence of the "if" operator. From the little test below, it seem to have a lower precedence than "or". Thus, it is desirable for the user to put the conditional expression in parentheses. Colin W.
15
1806
by: Nicholas M. Makin | last post by:
I was just thinking that I understood the conditional operator when I coded the following expecting it to fail: int a= 10, b= 20, c= 0; ((a < b) ? a : b) = c; // a=0 a=20; b= 10; ((a < b) ? a : b) = c; //b=0 Now since the expresion 5 = c is not valid I did not expect the above to work. But it does work. Why? I thought that the ?: operator would evaluate
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9962
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.