Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 382
WBAR Avatar asked Oct 08 '12 20:10

WBAR


People also ask

How do you print a return value in Python?

To print a value in Python, you call the print() function. Returning is used to return a value from a function and exit the function. To return a value from a function, use the return keyword.

How do you return a keyword in Python?

A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned.

What is the default return value of a function without a return statement in Python?

If a function doesn't specify a return value, it returns None . In an if/then conditional statement, None evaluates to False.


1 Answers

Easy peasy:

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

It's a conditional expression.

like image 128
Martijn Pieters Avatar answered Sep 27 '22 19:09

Martijn Pieters