Pythonic way for `return (value == 'ok') ? 'ok' : 'nok' `

Question:

Possible Duplicate:
Ternary conditional operator in Python

I have this problem and have no idea to ask google for this:

(value == 'ok') ? 'ok' : 'not ok'

I mean that grammar with:

(expression) ? (return if <expresion> is true) : (return this value if <expresion> is false
Asked By: WBAR

||

Answers:

Easy peasy:

'String ok' if value == 'ok' else 'String nok'

It’s a conditional expression.

Answered By: Martijn Pieters

How about this case:

{True: 'String ok', False: 'String nok'}[value == 'ok']

*Do not take seriously 🙂

Answered By: defuz
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.