Problem: How to return from a Python function or method in single line?
Example: Consider the following “goal” statement:
def f(x): return None if x == 0
However, this leads to a Syntax error:
In this tutorial, you’ll learn how to write the return statement with an if expression in a single line of Python code. You can get an overview of the three methods in the interactive code shell:
Exercise: The code has no output. Print the results of all three function executions for a given x. Is it always the same?
Let’s dive into the three methods.
Method 1: As a Multi-Liner
The following method is the standard and most Pythonic way to accomplish this but using multiple lines:
def f(x): if x==0: return None
But how to write this as a one-liner?
Method 2: Direct One-Liner If
Nothing simpler than that—just write it into a single line!
def f(x): if x==0: return None
I should note that PEP 8 is actually fine with writing if block statements into a single line. Nevertheless, the default return value of a function is
None so the code does really nothing.
Method 3: Ternary Operator
def f(x): return None if x==0 else 42
In this case, you also have to define a return value for the value 42. You should read the statement like this:
return (None if x == 0 else 42)
The statement inside the parentheses returns either
42—depending on the condition
x == 0. If it is
True, the value
None is returned. If it is
False, the value 42 is returned.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.