e.g. D, if sum_p is not zero.
D = 1/sum_p if sum_p \
else 0
What if I want to return the value? Should I do:
return 1/sum_p if sum_p \
else 0
or just write
if...
else: return 0
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.
If you want to do a single statement conditional return, use:
This is not an
ifstatement. It’s a conditional expression. Think of it asThe
returnitself is outside the conditional.If the line is long enough you have to split it up, stylistically I’d recommend using an ordinary
ifstatement and tworeturnstatements.In general, don’t use
\as a line terminator. PEP-8 recommends you always use parenthesis: