Ternary Operator in Python - People got Clever

The ternary operator can be incredibly useful in numerous situations in Python, which is why I'm surprised that prior to Python 2.5, there was no standard way of using one. In this article, we'll cover the ternary operator that was added in Python 2.5 along with the numerous ways that people emulated the operator before then.

Python 2.5 or Greater

When PEP 308 was created, it finally added the ternary operator into the release of Python 2.5. Here is the syntax that Guido chose:

x = trueValue if condition else falseValue

By doing it this way, it makes it easy to follow it as trueValue to be the normal and falseValue to be the less common case. Here is an example:

print "The temperature is %s freezing." % ("below" if temp < 32 else "above")

In this example, Python will print to the console "The temperature is below freezing." if the variable temp is less than 32. Otherwise, it prints "The temperature is below freezing."

Python 2.4 or Less

People actually got rather clever and managed to create two ternary "operators" before Python 2.5 came to be. One relies on tuples and the other relies on rather funky logic.

x = (falseValue, trueValue)[condition]

The above code is the way I see used more out in the wild with older scripts. It works based on the fact that a condition evaluated to True or False, which is equivalent to 1 or 0 respectively. Once the condition is evaluated, it gets used as an index for the tuple being used. Here is another less common way:

x = condition and trueValue or falseValue

This version works because if the condition evaluates to True (or 1), then trueValue will be chosen since 'anding' the two won't return False, and thus will select the trueValue. The opposite is true when the 'anding' results in False, at which point the other value in the 'or' will be chosen. This version is highly discouraged because it is impossible to have trueValue be equal to False since it'll break the logic. There are a few workarounds for this, but it only makes it more confusing.

Background

In my opinion, he chose the best of those that were available. It continues Python's long history of readability and the zen of python (if you don't know what that is, type 'import this' into the python console); however, there were numerous styles that the community proposed:

if C then x else y
(if C: x else: y)
C ? x : y
C ? x ! y
cond(C, x, y)
C then x else y
C ? x else y
C -> (x, y)

Yes, yes, I know. Most of these are ugly and few actually represent what a Python junkie would enjoy using, which is why I'm glad that Guido chose what he did over the standard in most languages:

x = (condition)? trueValue : falseValue

 

 

Leave a comment

Take Albuterol Cheap
Inhaler Buying Albuterol
Drug Buying Albuterol
Order Cheapest Albuterol
Order Albuterol Free Delivery
Buying Without Prescription Aldactone
Buy Generic Aldactone
Order Aldactone Next Day Delivery
Purchase Aldactone Online no Prescription
Buy Tablets Aldactone
Take no Prescription Allopurinol
Purchase Allopurinol Drug
When to Take Allopurinol
Order Allopurinol Free Delivery
Pills Buying Allopurinol
Drug Buying Anafranil
Alternative Purchase Anafranil
Purchase Anafranil Drug
Buy Tablets Anafranil Online
Order no Prescription Anafranil
Pills Buying Atarax
Cheapest Order Atarax
Buying Without a Prescription Atarax Online
Buy Atarax Medication
Atarax 50
Avodart Finasteride
Take Avodart Legally
Purchase Avodart by Phone
Tablets Purchase Avodart
Why Take Avodart
Order Bactrim Generic
Buy Without a Prescription Bactrim
Take Bactrim Next Day Delivery
Buying Bactrim Overnight Delivery
Take Bactrim Medication
Why Take Celecoxib
Order Celecoxib Free Delivery
Order Celecoxib Next Day Delivery
Buy Cheap Celecoxib
Purchase no Prescription Celecoxib
Cheapest Buying Cipro
Order Cipro Free Delivery
Buy Without a Prescription Cipro
Buy Cipro Tablets
Purchase Cipro Free Delivery
Order Clomid Serophene Without a Prescription
Order Clomid Serophene Medication
Online Order Clomid Serophene Without Prescription
Buy Clomid Serophene Medication
Purchase Cheap Clomid Serophene
Buy Dapsone Without a Prescription
Alternative Order Dapsone
Buy Dapsone by Phone
Buy Dapsone Overnight Delivery
Buy Dapsone Tablets
Buy Desyrel Online no Prescription
Purchase Desyrel Next Day Delivery
Order Desyrel Free Delivery
Buy Desyrel Next Day Delivery
Purchase Cheapest Desyrel
Take Diflucan Medication
Purchase Diflucan Free Delivery
Tablets Buying Diflucan
Buy Diflucan Pills
Order Cheap Diflucan
Alternative Purchase Dilantin
Buying Without a Prescription Dilantin Online
Buy no Prescription Dilantin Online
Purchase no Prescription Dilantin
Cheapest Order Dilantin
Buy Tablets Diovan
Buy Diovan Free Delivery
Purchase Diovan Generic
Buying Pills Diovan
Buying Without a Prescription Diovan Online
Generic Order Doxycycline
Buy Tablets Doxycycline
Buy Doxycycline Overnight Delivery
Buy Tablets Doxycycline Online
Purchase Pills Doxycycline
Drug Order Elavil
Buy Elavil Overnight Delivery
Buy Elavil Without a Prescription
Take Elavil Legally
Buy Alternative Elavil
Purchase Online Without a Prescription Erythromycin
Purchase Erythromycin Without a Prescription
Generic Order Erythromycin
Pills Buying Erythromycin
Purchase Cheapest Erythromycin
Take Estrace Free Delivery
Pills Buying Estrace
Generic Buying Estrace
Purchase Online no Prescription Estrace
Order Estrace by Phone
Buy Furosemide Medication
Generic Buying Furosemide
Buying Furosemide On-Line
Buy Without a Prescription Furosemide
Take Furosemide Drug
Take Glucophage Next Day Delivery
Tablets Purchase Glucophage
Order Pills Glucophage
Order Glucophage Generic
Order Cheapest Glucophage
Purchase Imitrex Online no Prescription
Take Imitrex Without Prescription
Order Imitrex Medication
Buy Cheap Imitrex
Online Order Imitrex Without Prescription
Buy no Prescription Inderal
Pills Buying Inderal
Order Inderal Without a Prescription
Buying Inderal On-Line
Order Inderal Medication
Lasix 40
Buying Lasix On-Line
Buy Lasix no Prescription
Order Online Without a Prescription Lasix
Buy Lasix Generic
Take Levaquin Drug
Order Levaquin Pills
Buy Tablets Levaquin
Buying Without Prescription Levaquin
Purchase Cheapest Levaquin
Take Lexapro Daily Dose
Order Pills Lexapro
Take Lexapro Legally
Take Lexapro Drug
Purchase Generic Lexapro
Order Lipitor COD
Buy Pills Lipitor
Purchase Online Without a Prescription Lipitor
Lipitor Lipidor
Buy Cheapest Lipitor
Purchase Generic Lisinopril
Buy Lisinopril Tablets
Generic Buying Lisinopril
Purchase Lisinopril Online Without Prescription
Purchase Lisinopril Medication
Purchase Methotrexate Medication
Take Methotrexate Pills
Order Methotrexate Generic
Order Methotrexate Without a Prescription
Buy Tablets Methotrexate Online
Order Nizoral Pills
Order Nizoral Free Delivery
Order Nizoral no Prescription
Pills Buying Nizoral
Buy no Prescription Nizoral Online
Buy Nolvadex Cheapest
Alternative Order Nolvadex
Buying Nolvadex Free Delivery
Purchase Nolvadex COD
Alternative Purchase Nolvadex
Online Order Nortriptyline Without Prescription
Buying Without Prescription Nortriptyline
Purchase no Prescription Nortriptyline
Purchase Nortriptyline Online Without Prescription
Purchase Nortriptyline by Phone
Alternative Purchase Ortho Tri-Cyclen
Buy Ortho Tri-Cyclen Medication
Buying Ortho Tri-Cyclen Next Day Delivery
Online Buy Ortho Tri-Cyclen Without a Prescription
Purchase Ortho Tri-Cyclen Generic
Buy Tablets Premarin
Order Premarin Generic
Order Premarin Free Delivery
Purchase Pills Premarin
Purchase Premarin Next Day Delivery
When to Take Proscar
Order Proscar Free Delivery
Take Proscar Free Delivery
Cheapest Buy Proscar
Purchase Proscar Next Day Delivery
Online Order Risperdal Without Prescription
Buy Risperdal Without a Prescription
Generic Order Risperdal
Buying Risperdal Next Day Delivery
Buy Online Without a Prescription Risperdal
Buying Synthroid Without Prescription
Alternative Buying Synthroid
Order Synthroid by Phone
Buy Alternative Synthroid
Buy Online Without a Prescription Synthroid
Order Topamax Pills
Tablets Order Topamax
Order Cheap Topamax
Order Topamax Generic
Take Topamax Daily Dose
Purchase Valtrex COD
Buy no Prescription Valtrex
Buy Pills Valtrex Online
Tablets Buy Valtrex
Buy Valtrex COD
Legally Order Zithromax
Tablets Order Zithromax
Buy Without a Prescription Zithromax
Buying Without a Prescription Zithromax Online
Purchase Zithromax Without a Prescription