Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
The ternary operator in Python

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)


Back to
Think about your design even if you don't use full UML
Previous and next
or
Horse's mouth home
Forward to
Keeping the visitors happy and browsing
Some other Articles
Dressed up for a phone call
Stopping and restarting Apache httpd cleanly
Web Application Components
Keeping the visitors happy and browsing
The ternary operator in Python
Think about your design even if you don't use full UML
Can some food be TOO different?
Making use of disabled facilities
Easy feed!
Morning Post
2618 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2010: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho