You may know the ternary operator
x if c else y in Python which reads as “return
c else return
y“. But you may not know that you can also write the ternary operator in a shorter form as
(y, x)[c]. Let’s dive into this concise way of hacking your own ternary operator!
A shorthand form of the ternary operator is the following tuple syntax hack.
Syntax: You can use the tuple syntax
(x, y)[c] consisting of a tuple
(x, y) and a condition
c enclosed in a square bracket. Here’s a more intuitive way to represent this tuple syntax.
|<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 |
Exercise: Run the code and compare both ternary operators—the original and the tuple syntax hack.
In fact, the order of the
<OnTrue> operands is just flipped when compared to the basic ternary operator. First, you have the branch that’s returned if the condition does NOT hold. Second, you run the branch that’s returned if the condition holds.
age = 17 print(('wtf', 'What?')[age<20]) 'What?'
age<20 holds so the return value passed into the
print() function is the
'What?'. Don’t worry if this confuses you—you’re not alone. Let’s clarify why this tuple syntax works the way it does!
First, you create a tuple
('wtf', 'What?'). To access the first tuple value
'wtf', you’d use the standard indexing syntax
('wtf', 'What?'). To access the second tuple value
'What?', you’d use the standard indexing syntax
Second, you create a condition
age<20. You use this condition as the indexing value. You end up with either
('wtf', 'What?')[False] or
('wtf', 'What?')[True]. As you may know, the Booleans
True are represented through integers
1 in Python. Thus, you get
('wtf', 'What?') and
('wtf', 'What?'), respectively.
In other words: if your condition evaluates to
False, you access the first tuple value. If your condition evaluates to
True, you access the second tuple value.
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.