[Tutor] 2.5's new conditional expression syntax

wesley chun wescpy at gmail.com
Fri Sep 29 00:25:01 CEST 2006


On 9/28/06, Dick Moores <rdm at rcblue.com> wrote:
> I've been looking hard at 2.5's new conditional expression syntax
> (<http://docs.python.org/whatsnew/pep-308.html>), and didn't
> understand the examples there...
>     :
> But it would help to see an example I could understand that also
> shows the syntax's usefulness. Could someone cook up one for me?

here is an example from "Core Python:"

WITHOUT conditional expressions:

>>> x = 4
>>> y = 3
>>> if x < y:
... smaller = x
...    else:
... smaller = y
...
>>> smaller
3

WITH conditional expressions:
>>> smaller = x if x < y else y
>>> smaller
3

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list