Create an account


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

#1
Python Nested Multiple Ternary Operators

In which order does the nested ternary operator evaluate its conditions?

Short Answer: The nested ternary operator '1' if x else '2' if y else '3' evaluates the condition from left to right, i.e., '1' if x else ('2' if y else '3'). In short, first condition first!

Python Nested Multiple Ternary Operators

Problem Formulation


Given a nested ternary operator in Python:

'1' if x else '2' if y else '3'

Question: What is the operator precedence for the nested ternary operator?

  • Scenario A: ('1' if x else '2') if y else '3'
  • Scenario B: '1' if x else ('2' if y else '3')

Scenario A is semantically different to Scenario B as can be seen in this example:

>>> x, y = True, False
>>> # Scenario A
>>> ('1' if x else '2') if y else '3' '3'
>>> # Scenario B
>>> '1' if x else ('2' if y else '3') '1'

Simple Nesting


The ternary operator evaluates from left to right. If the ternary operator is nested such as in '1' if x else '2' if y else '3', Python first evaluates the left condition before it evaluates the right condition.

>>> x, y = True, False # Scenario A
>>> ('1' if x else '2') if y else '3' '3' # Scenario B
>>> '1' if x else ('2' if y else '3') '1' # Correct Scenario: B
>>> '1' if x else '2' if y else '3' '1'

Multiple Nesting


What if the ternary operator has multiple nesting levels like so:

'1' if x else '2' if y else '3' if z else '4'

Again, the ternary operator evaluates from left to right. Even if the ternary operator has more than two nesting levels such as in '1' if x else '2' if y else '3' if z else '4', Python first evaluates the left condition before it evaluates the second-left condition before it evaluates the third-left condition.

You can see this in the following example:

>>> x, y, z = True, True, False >>> # Scenario A
>>> (('1' if x else '2') if y else '3') if z else '4' '4' >>> # Scenario B
>>> '1' if x else ('2' if y else ('3' if z else '4')) '1' >>> # Correct Scenario: B
>>> '1' if x else '2' if y else '3' if z else '4' '1'

How to Manipulate the Operator Precedence?


You can manipulate the normal operator precedence for the ternary operator by using the parentheses accordingly. By enclosing a term in a parentheses, you force Python to evaluate the expression in the specified order. This way, you can force Python, for example, to evaluate the second condition before the first condition of the ternary operator.

Here’s an example:

>>> x, y = True, False
>>> # Normal precedence: first condition first
>>> '1' if x else '2' if y else '3' '1'
>>> # Force second condition first:
>>> ('1' if x else '2') if y else '3' '3'

How Does the Ternary Operator Work Anyway?


The most basic ternary operator x if c else y consists of three operands x, c, and 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 y.




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

<OnTrue> if <Condition> else <OnFalse>

Operand Description
<OnTrue> The return expression of the operator in case the condition evaluates to True
<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 False

Operands of the Ternary Operator

Read more in our detailed guide here:



https://www.sickgaming.net/blog/2021/12/...operators/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
[Tut] How To Apply A Function To Each El...
Last Post: xSicKxBot
Today 04:46 PM
» Replies: 0
» Views: 0
(Indie Deal) Exotic Riddles Bundle, Dyin...
Last Post: xSicKxBot
Today 04:45 PM
» Replies: 0
» Views: 0
Mobile - Pokémon Sword and Shield myster...
Last Post: xSicKxBot
Today 04:45 PM
» Replies: 0
» Views: 0
.NET Framework January 2022 Cumulative U...
Last Post: xSicKxBot
Today 04:45 PM
» Replies: 0
» Views: 0
AppleInsider - Philips Hue gains trio of...
Last Post: xSicKxBot
Today 04:45 PM
» Replies: 0
» Views: 0
Fedora - Quarkus and Mutiny
Last Post: xSicKxBot
Today 04:45 PM
» Replies: 0
» Views: 0
News - Pokémon Unite Adjustments Arrive ...
Last Post: xSicKxBot
Today 04:45 PM
» Replies: 0
» Views: 0
Xbox Wire - Besiege Console (Game Previe...
Last Post: xSicKxBot
Today 04:45 PM
» Replies: 0
» Views: 0
News - Vulkan 1.3 Specification Released
Last Post: xSicKxBot
Today 04:45 PM
» Replies: 0
» Views: 0
(Indie Deal) Start saving early with Gea...
Last Post: xSicKxBot
Yesterday 05:23 PM
» Replies: 0
» Views: 42

[-]
Twitter



Discord Server © SickGaming.net 2012-2021