The ? : operator that you may have come across in Perl, PHP, C and Java - known as the ternary or conditional operator - is ABSENT from Python.
"But it's so useful" I hear you cry. Ah yes, but isn't this elegant:
val = float(raw_input("Age: "))
status = ("working","retired")[val>65]
print "You should be",status
UPDATE ... There IS now a "ternary" operator in Python (recent versions of Python 2, and Python 3) ... this post was orginally made in March 2006 ...
Footnote - the ? : operator in those other languages gives you a very convening short form of if, then, else where you can select a value based on a condition for printing or assigning to a variable; here's a Perl or PHP example:
$status = ($val > 65) ? "retired" : "working";
Great, useful, but yet another oddball structure on the language - I'm forever having to explain how an operator can have three operands and how you MUST have a : if you have a ? ... but in Python it's so natural to use a tuple ...
(written 2006-03-25 17:18:24)
Associated topics are indexed under
H104 - PHP - Control Statements [2304] Extracting real data from an exported file in PHP or Perl - (2009-07-25)
[1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1199] Testing for one of a list of values. - (2007-05-22)
[1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
[863] Double and Triple equals operator in PHP - (2006-09-12)
[421] Don't repeat code - use loops or functions - (2005-08-21)
[406] Assignment, equality and identity in PHP - (2005-08-08)
[353] Wimbledon Neck - (2005-06-20)
[340] Code and code maintainance efficiency - (2005-06-08)
P206 - Perl - More Loops and Conditionals [1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[299] What - no switch or case statement? - (2005-05-03)
[138] Perl - redo and last without a loop - (2004-12-02)
Y103 - Python - Conditionals and Loops [1661] Equality, sameness and identity - Python - (2008-05-31)
[1201] No switch in Python - (2007-05-23)
[909] Python is like a narrowboat - (2006-10-30)
[835] Python - when to use the in operator - (2006-08-16)
[788] New - Conditional expressions in Python 2.5 - (2006-07-01)
[668] Python - block insets help with documentation - (2006-04-04)
Y104 - Python - Lists and Tuples [2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2284] Strings as collections in Python - (2009-07-12)
[2280] Creating and iterating through Python lists - (2009-07-12)
[1789] Looking for a value in a list - Python - (2008-09-08)
[1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
[955] Python collections - mutable and imutable - (2006-11-29)
[899] Python - extend v append on a list - (2006-10-20)
[383] Overloading of operators on standard objects in Python - (2005-07-19)
Some other Articles
Dressed up for a phone callStopping and restarting Apache httpd cleanlyWeb Application ComponentsKeeping the visitors happy and browsingThe ternary operator in PythonThink about your design even if you don't use full UMLCan some food be TOO different?Making use of disabled facilitiesEasy feed!Morning Post