Question from a python newbie

Remco Gerlich remco at gerlich.nl
Thu Dec 13 10:57:04 EST 2007


On Dec 13, 2007 4:39 PM, Russell <rcooknet at gmail.com> wrote:

> I've been learning Python slowly for a few months, coming from a C/C+
> +, C#, Java, PHP background.  I ran across a code fragment I'm having
> trouble wrapping my brain around.  I've searched the Language
> Reference and was not able to find any info regarding the structure of
> this code fragment:
>
> int(text) if text.isdigit() else text


Hi,

It's a pretty new construct; basically it's Python's version of the ? :
"ternary operator" in other languages.

The line above is equivalent to

text.isdigit() ? int(text) : text

in, for instance, C.

Remco Gerlich
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071213/99ba9a1f/attachment.html>


More information about the Python-list mailing list