I have the following Code in Python:
def show_sequence(n): if n > 0: return "+".join((str(i) for i in range(n+1))) + " = %d" %(sum(range(n+1))) elif n == 0: return "0=0" else: return str(n) + "<0" Question: Is there a syntax correct way of putting all lines into one return statement if there are 3 if-statements? I know it works with one if- & else-statement but im a fan of one-liner and already asked this myself several times.
return "+".join(...) if n > 0 else "0=0" if n == 0 else str(n) + "<0". (But if anyone else has to read your code, please don't inflict this kind of syntax abuse on them).n > 0andn == 0into a singlen >= 0case, as"+".join([0]) == "0"andsum([0]) == 0.