How to write the expression shorter:
return '%.0f' % float_var if float_var else float_var
or
if float_var:
return formatted_string
else:
return None
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The expression
<value> if <condition> else <other_value>is pretty idiomatic already — certainly more so than the other example, and is probably preferred whenever<value>is simple. This is Python’s ternary operator, so if you were looking for something like<condition> ? <value> : <other_value>, that doesn’t exist.If computing
<value>or<other_value>takes a few steps, use the longerif: ... else: ...alternative.