Teaching Programming

James Mills prologic at shortcircuit.net.au
Tue May 4 08:04:19 EDT 2010


On Tue, May 4, 2010 at 9:43 PM, Stefan Behnel <stefan_ml at behnel.de> wrote:
>> Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
>> [GCC 4.3.3] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>  >>> A,B=2,3
>>  >>> if A>B:
>> ... print A+B
>> ... else:
>> ... print A**B-B**2
>> ...
>> -1
>>  >>> A,B=3,2
>>  >>> if A>B:
>> ... print A+B
>> ... else:
>> ... print A**B-B**2
>> ...
>> 5
>>
>> tell me please: how can generate the same output (depending on A and B)
>> without control structure? i mean in a natural "pythonic" way...

>>> def quadratic(a, b):
...     return a + b if a > b else a**b - b**2
...
>>> a, b = 2, 3
>>> print quadratic(a, b)
-1
>>> a, b = 3, 2
>>> print quadratic(a, b)
5
>>>

--james



More information about the Python-list mailing list