From: superpollo on
James Mills ha scritto:
> On Tue, May 4, 2010 at 9:56 PM, superpollo <utente(a)esempio.net> wrote:
>> of course! *but* if i must generate on-the-fly python code that defines a
>> function i am back again to the problem:
>
> One-liner:
>
> $ python
> Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49)
> [GCC 4.4.1 (CRUX)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> a, b = 2, 3
>>>> print a + b if a > b else a**b - b**2
> -1
>>>> a, b = 3, 2
>>>> print a + b if a > b else a**b - b**2
> 5
>
> --James

what if i want an elif clause?

From: superpollo on
superpollo ha scritto:
> James Mills ha scritto:
>> On Tue, May 4, 2010 at 9:56 PM, superpollo <utente(a)esempio.net> wrote:
>>> of course! *but* if i must generate on-the-fly python code that
>>> defines a
>>> function i am back again to the problem:
>>
>> One-liner:
>>
>> $ python
>> Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49)
>> [GCC 4.4.1 (CRUX)] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> a, b = 2, 3
>>>>> print a + b if a > b else a**b - b**2
>> -1
>>>>> a, b = 3, 2
>>>>> print a + b if a > b else a**b - b**2
>> 5
>>
>> --James
>
> what if i want an elif clause?
>

my first try obviously falied:

>>> print a + b if a > b elif a=b "WOW" else a**b - b**2
File "<stdin>", line 1
print a + b if a > b elif a=b "WOW" else a**b - b**2
^
SyntaxError: invalid syntax

From: Terry Reedy on
On 5/4/2010 1:44 PM, Dennis Lee Bieber wrote:
> On Tue, 04 May 2010 12:06:10 -0400, Terry Reedy<tjreedy(a)udel.edu>
> declaimed the following in gmane.comp.python.general:
>
>> Speak for yourself, please. For two decades before I met Python, I
>> indented code nicely whenever it was allowed. That option was one of the
>> great advancements of Fortran77 over FortranIV. Coming from C, I was
>> immediately glad to be done with those darn braces.
>>
> What kept you from indenting FORTRAN-IV?

There was a several year gap with learning of 'structured programming'
in between. Perhaps ignorance of both the possibility (no one did it,
that I know of) and desirability. Glass screens made consistent
indentation a lot easier that with cards and teletypes. The new
structured block constructs made indentation more useful, too.



From: superpollo on
superpollo ha scritto:
> superpollo ha scritto:
>> James Mills ha scritto:
>>> On Tue, May 4, 2010 at 9:56 PM, superpollo <utente(a)esempio.net> wrote:
>>>> of course! *but* if i must generate on-the-fly python code that
>>>> defines a
>>>> function i am back again to the problem:
>>>
>>> One-liner:
>>>
>>> $ python
>>> Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49)
>>> [GCC 4.4.1 (CRUX)] on linux2
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>> a, b = 2, 3
>>>>>> print a + b if a > b else a**b - b**2
>>> -1
>>>>>> a, b = 3, 2
>>>>>> print a + b if a > b else a**b - b**2
>>> 5
>>>
>>> --James
>>
>> what if i want an elif clause?
>>
>
> my first try obviously falied:
>
> >>> print a + b if a > b elif a=b "WOW" else a**b - b**2
> File "<stdin>", line 1
> print a + b if a > b elif a=b "WOW" else a**b - b**2
> ^
> SyntaxError: invalid syntax
>

ok i got it:

>>> a,b=2,3
>>> print a + b if a > b else "WOW" if a < b else a**b - b**2
WOW
>>>
From: Benjamin Kaplan on
On Tue, May 4, 2010 at 2:16 PM, superpollo <utente(a)esempio.net> wrote:
> superpollo ha scritto:
>>
>> James Mills ha scritto:
>>>
>>> On Tue, May 4, 2010 at 9:56 PM, superpollo <utente(a)esempio.net> wrote:
>>>>
>>>> of course! *but* if i must generate on-the-fly python code that defines
>>>> a
>>>> function i am back again to the problem:
>>>
>>> One-liner:
>>>
>>> $ python
>>> Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49)
>>> [GCC 4.4.1 (CRUX)] on linux2
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>>
>>>>>> a, b = 2, 3
>>>>>> print a + b if a > b else a**b - b**2
>>>
>>> -1
>>>>>>
>>>>>> a, b = 3, 2
>>>>>> print a + b if a > b else a**b - b**2
>>>
>>> 5
>>>
>>> --James
>>
>> what if i want an elif clause?
>>
>
> my first try obviously falied:
>
>>>> print a + b if a > b elif a=b "WOW" else a**b - b**2
>  File "<stdin>", line 1
>    print a + b if a > b elif a=b "WOW" else a**b - b**2
>                            ^
> SyntaxError: invalid syntax

That's because this technically isn't an if statement. It's the
ternary conditional expression. If you want multiple conditions, you
have to repeat the expression

print a+b if a > b else "WOW" if a==b else a**b - b**2


>
> --
> http://mail.python.org/mailman/listinfo/python-list
>