Ternary Operator in Python

Roy Smith roy at panix.com
Fri Apr 1 18:53:14 EST 2005


In article <1112391650.018826.274970 at l41g2000cwc.googlegroups.com>,
 "Carl Banks" <invalidemail at aerojockey.com> wrote:

> Terry Reedy wrote:
> > "praba kar" <prabapython at yahoo.co.in> wrote in message
> > news:20050401072442.73412.qmail at web8408.mail.in.yahoo.com...
> > > Dear All,
> > >    I am new to Python.  I want to know how to
> > > work with ternary operator in Python.  I cannot
> > > find any ternary operator in Python.  So Kindly
> > > clear my doubt regarding this
> >
> > A unary operator has one operand; a binary operator has two operands;
> a
> > ternary operator has three operands.  Python has none built-in,
> 
> Not so fast, my friend.  What about the expression "0.0 < a < 1.0"?

I still remember one of the earliest bugs I ever wrote (I've long since 
forgotten most of the zillions I've written since).  It must have been 
around 1975, and my high school had an ASR-33 connected to a HP-3000 
running Time Shared Basic at another school a few towns away.

I wrote something like "1 < X < 10" and got an error.  I was puzzled by 
this, since we were using this notation in math class.  The answer of 
course was that I needed to write "1 < X AND X < 10", which I found really 
annoying and strange looking.  Or is my long-term memory returning 
corrupted data?  Maybe BASIC let you do 1 < X < 10, but I ran into this 
when I moved onto FORTRAN the next year?

In any case, I've gotten so used to writing 1 < x && x < 10 (or variations 
on the theme) that now I've got a language which lets me write it the 
normal math way, 1 < x < 10, and *that* looks strange.  Wierd, huh?  How 
our tools warp our thinking.



More information about the Python-list mailing list