Python ternary operator

Question:

How can I do something like var foo = (test) ? "True" : "False"; in Python (specifically 2.7)?

Asked By: cynicaljoy

||

Answers:

PEP 308 adds a ternary operator:

foo = "True" if test else "False"

It’s been implemented since Python 2.5

Answered By: Brian McKenna

This one looks a bit more like original ternary:

foo=a and b or c
Answered By: Frost.baka
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.