Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Ternary Multiple Lines
1
Python Ternary Multiple Lines

What if you have a ternary operator that’s very long?

Code:
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
Code:
x if c else y
consists of three operands
Code:
x
,
Code:
c
, and
Code:
y
. It is an expression with a return value. The ternary operator returns
Code:
x
if the Boolean expression
Code:
c
evaluates to
Code:
True
. Otherwise, if the expression
Code:
c
evaluates to
Code:
False
, the ternary operator returns the alternative
Code:
y
.

Syntax: The three operands are written as
Code:
x if c else y
which reads as “return
Code:
x
if
Code:
c
else return
Code:
y
“. Let’s write this more intuitively as:

Code:
<OnTrue> if <Condition> else <OnFalse>

Operand |
Description

|
The return expression of the operator in case the condition evaluates to
Code:
True

|
The condition that determines whether to return the or the branch.

|
The return expression of the operator in case the condition evaluates to
Code:
False

[i]Operands of the Ternary Operator[/i]



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.

Code:
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 PEP8standard 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:

https://trinket.io/embed/python/4ff1b0b336

Exercise: Write a nested ternary operator and break it into multiple lines!

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!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: