Ternary in Python 2.5

Python 2.5 features a new ternary expression. Here is an example to clarify how it works:

In[1]: boolean=False

In[2]: answer = "yesss" if boolean==False else "noo"

In[3]: answer
Out[3]: 'yesss'

In[4]: answer = "yesss" if boolean==True else "noo"

In[5]: answer
Out[5]: 'noo'

Summary:
variabel = [expression-1] if [boolean-condition] else [expression-2]

Reference: http://www.python.org/dev/peps/pep-0308/

Comments

Post new comment

  • Allowed HTML tags: <strong> <code>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options