[Tutor] 2.5's new conditional expression syntax
rdm at rcblue.com
Fri Sep 29 01:08:49 CEST 2006
At 03:25 PM 9/28/2006, wesley chun wrote:
>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
>... smaller = y
>WITH conditional expressions:
>>>>smaller = x if x < y else y
>hope this helps!
That's very clear. Thanks, Wesley.
And if you wanted to know which is smaller, x or y:
x = 8**9
y = 9**8
smaller = "x" if x < y else "y"
print smaller, "is smaller"
I think I'm getting the hang of it!
BTW I'm told your book will arrive on my front porch in a couple of
days. I'm looking forward to it! (<http://tinyurl.com/f22uu>)
More information about the Tutor