Python has always been described as a "tight" language in that new syntaxes are only added with a great deal of thought, and then only if they provide a significant benefit without diluting the crispness of the language.
There have been long discussions in the Python world about adding in a conditional operator - something that gives and easier form to an if / else construct that's used only to set a variable to one of two alternative values. Now, in
release 2.5 which went out to Beta release in the last few days, such a facility has been added.
Suppose you have a variable called
stock_level and you want to set up another variable (for output) with the word "is" or "are" in it, depending on whether the stock level is 1 or something else, you can write (as from Python 2.5):
word = "is" if stock_level == 1 else "are"
which is much shorter that the alternative so far available:
if stock_level == 1:
word = "is"
else:
word = "are"
For anyone with a Perl, PHP, C or Java backgrond, this new syntax is the moral equivalent of the ? : operator. After long discussion, Guido van Rossum decided for the syntax shown above ... amongst the reasons for this surprising choice are the fact that the ":" character is already very significant in Python and although the syntax rules would have allowed Python to follow the other languages, resultant code would not have been clear and easy to follow. (written 2006-07-01, updated 2006-07-04)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y103 - Python - Conditionals and Loops [299] What - no switch or case statement? - (2005-05-03)
[353] Wimbledon Neck - (2005-06-20)
[657] The ternary operator in Python - (2006-03-25)
[668] Python - block insets help with documentation - (2006-04-04)
[835] Python - when to use the in operator - (2006-08-16)
[909] Python is like a narrowboat - (2006-10-30)
[1201] No switch in Python - (2007-05-23)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1661] Equality, sameness and identity - Python - (2008-05-31)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
[2899] Groupsave tickets - 3 or 4 train tickets for the price of 2 - (2010-08-02)
[3083] Python - fresh examples from recent courses - (2010-12-11)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
[3558] Python or Lua - which should I use / learn? - (2011-12-21)
[3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
[3762] Learning to program - the if statement. Python. - (2012-06-12)
[3895] Flowchart to program - learning to program with Well House - (2012-10-14)
[4092] Identity in Python - (2013-05-17)
[4210] If elif elif elif - multiway selection in Python - (2013-11-16)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
[4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
[4541] Setting up and tearing down with the Python with keyword - (2015-10-16)
[4545] Method, Class, Module, Package - how to they relate in Python? - (2015-10-17)
[4713] Equality (in Python) - (2016-10-30)
[4723] Conditional operators in Python - (2016-11-05)
Some other Articles
Is Java the right language to learn?Strange housewarmingShadow Transport Minister to visit MelkshamHot answers in PHPNew - Conditional expressions in Python 2.5Tk - laying out your GUI with frames, pack and gridFirst Light, Bootle Docks, LiverpoolRunning external processes in Tcl and Tcl/TkWhich way to turn?Good follow up ... my thanks