07-12-2020, 05:12 AM
Python Ternary — Tuple Syntax Hack
You may know the ternary operator in Python which reads as “return if else return “. But you may not know that you can also write the ternary operator in a shorter form as . Let’s dive into this concise way of hacking your own ternary operator!
![[Image: tuple-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/07/tuple-1024x576.jpg)
A shorthand form of the ternary operator is the following tuple syntax hack.
Syntax: You can use the tuplesyntax consisting of a tuple and a condition enclosed in a square bracket. Here’s a more intuitive way to represent this tuple syntax.
Operand |
Description
|
The return expression of the operator in case the condition evaluates to
|
The condition that determines whether to return the or the branch.
|
The return expression of the operator in case the condition evaluates to
[i]Tuple Syntax of the Ternary Operator[/i]
https://trinket.io/embed/python/8b1118a41c
Exercise: Run the code and compare both ternary operators—the original and the tuple syntax hack.
In fact, the order of the and 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.
The condition holds so the return value passed into the function is the branch . 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. To access the first tuple value , you’d use the standard indexing syntax . To access the second tuple value , you’d use the standard indexing syntax .
Second, you create a condition. You use this condition as the indexing value. You end up with either or . As you may know, the Booleans and are represented through integers and in Python. Thus, you get and , respectively.
In other words: if your condition evaluates to, you access the first tuple value. If your condition evaluates to , you access the second tuple value.
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become 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.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
You may know the ternary operator
Code:
x if c else yCode:
xCode:
cCode:
yCode:
(y, x)[c]![[Image: tuple-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/07/tuple-1024x576.jpg)
A shorthand form of the ternary operator is the following tuple syntax hack.
Syntax: You can use the tuplesyntax
Code:
(x, y)[c]Code:
(x, y)Code:
cCode:
(<OnFalse>, <OnTrue>)[<Condition>]Operand |
Description
The return expression of the operator in case the condition evaluates to
Code:
TrueThe condition that determines whether to return the
The return expression of the operator in case the condition evaluates to
Code:
False[i]Tuple Syntax of the Ternary Operator[/i]
https://trinket.io/embed/python/8b1118a41c
Exercise: Run the code and compare both ternary operators—the original and the tuple syntax hack.
In fact, the order of the
Code:
<OnFalse>Code:
<OnTrue>Code:
age = 17
print(('wtf', 'What?')[age<20]) 'What?'The condition
Code:
age<20Code:
print()Code:
<OnTrue>Code:
'What?'First, you create a tuple
Code:
('wtf', 'What?')Code:
'wtf'Code:
('wtf', 'What?')[0]Code:
'What?'Code:
('wtf', 'What?')[1]Second, you create a condition
Code:
age<20Code:
('wtf', 'What?')[False]Code:
('wtf', 'What?')[True]Code:
FalseCode:
TrueCode:
1Code:
('wtf', 'What?')[0]Code:
('wtf', 'What?')[1]In other words: if your condition evaluates to
Code:
FalseCode:
TrueWhere to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become 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.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!

