The conditional or ternary (because it involves three operands) operator is probably familiar to most programmers working with C-style programming languages. JavaScript adopts it right away:

  condExp ? thenExp : elseExp

All three operands are expressions, and the resulting expression means "If the condExp expression evaluates to true, return the value of the thenExp, otherwise that of the elseExp."

Until version 2.5 Python didn't have a ternary operator. When the operator was introduced it adopted a different approach to the syntax:

  thenExp if condExp else elseExp

but the semantics remain the same.

Prior to version 2.5 Python programmers would come up with all kinds of funny hacks to approximate this operator. Here are some of them:

  condExp and thenExp or elseExp           (1)

  (condExp and [thenExp] or [elseExp])[0]  (2)

  (thenExp, elseExp)[not condExp]          (3)

  (elseExp, thenExp)[bool(condExp)]        (4)

As you can see all cases involve logical operations, and some add sequence operations as well. You might want to ponder why this works and what the differences between the versions may be. For example, both (3) and (4) rely on the fact that not and bool turn True and False to 1 and 0 in list context, which is not entirely intuitive if you know that they are supposed to construct boolean values. (2) uses list construction ([...]) and deconstruction ([0]) to do basically the same as (1). The difference is rather subtle: In (1), if the condExp evaluates to true, the thenExp should be returned. But if the thenExp evaluates to false, the whole and expression evaluates to false too, and the elseExp is wrongly returned. (2) prevents this since a list containing only the False value is itself true.

Another general aspect of those approximations is that of evaluation of operands. In (1) and (2) the short-circuit evaluation of the logical operands assure that elseExp is never evaluated when condExp is true. In contrast, (3) and (4) evaluate both elements as part of the tuple construction before the accessor selects one of them. This may make a difference if they contain e.g. calls to function.