What if you have a ternary operator that’s very long?
var = 'I want to learn Python' if 42**2<166 else 'I want to learn Go programming' print(var) # I want to learn Go programming
Problem: How to write the ternary operator in multiple lines?
Short Recap: Ternary Operator
Ternary Operator: The most basic ternary operator
x if c else y consists of three operands
y. It is an expression with a return value. The ternary operator returns
x if the Boolean expression
c evaluates to
True. Otherwise, if the expression
c evaluates to
False, the ternary operator returns the alternative
Syntax: The three operands are written as
x if c else y which reads as “return
c else return
y“. Let’s write this more intuitively as:
<OnTrue> if <Condition> else <OnFalse>
|<OnTrue>||The return expression of the operator in case the condition evaluates to |
|<Condition>||The condition that determines whether to return the <On True> or the <On False> branch.|
|<OnFalse>||The return expression of the operator in case the condition evaluates to |
Related article: For a full tutorial on the ternary operator, check out our detailed blog article.
Method: Parenthesis to Extend Logical Line Over Multiple Physical Lines
Solution: You can extend any logical line in Python over multiple physical lines by using the parenthesis.
var = 'I want to learn Python' if 42**2<166 else 'I want to learn Go programming' print(var) var = ('I want to learn Python' if 42**2<166 else 'I want to learn Go programming') print(var) # I want to learn Go programming
This is the PEP8 standard way of breaking long lines—if you cannot do it in a more natural way (such as avoiding the ternary operator and using the if statement in this example).
Try it yourself:
Exercise: Write a nested ternary operator and break it into multiple lines!
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.