Python if else one liner

Instead of:

def func(x):
    if x == 0:
        return "something"
    else:
        return "something else"

Just do this:

def func(x):
    return "something" if x == 0 else "something else"

Nice to know and saved 3 lines of code!-)

Published: August 08 2013

  • tags:
blog comments powered by Disqus