Nifty, little one-liner I learned today. Instead of doing this:

def func(foo):
    if foo == 0:
        return "bar"
    else:
        return "baz"

You can simply do this:

def func(foo):
    return "bar" if foo == 0 else "baz"


Pretty cool for those who didn't know. ;-)

- Karan Goel
3

View comments

joe - A .gitignore magician in your command line
joe - A .gitignore magician in your command line
2
thekarangoel is now karan on Github
thekarangoel is now karan on Github
2
Sum of digits of a number in one line (or any other mutation)
Sum of digits of a number in one line (or any other mutation)
1
One-liner if..else statements.
One-liner if..else statements.
3
A list of 100+ projects to complete to master Python.
A list of 100+ projects to complete to master Python.
6
Blog Archive
About Me
About Me
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.