Python

Python Ternary Operator

Hello in this tutorial, we will understand the ternary operator in python programming.

1. Introduction

The ternary operator is used in programming languages to return output based on a binary condition. It is similar to the if-else condition block just a little neat. In python, it is represented by the below syntax –

Syntax

value_if_true if condition else value_if_false

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download Python from this link.

2. Ternary operator in python

I am using JetBrains PyCharm as my preferred IDE. You’re free to choose the IDE of your choice.

2.1 Ternary operator Implementation file

Add the below code to the implementation file showing the different examples in python. We will use three different approaches –

  • Single line example
  • Examples with –
    • Tuples
    • Dictionary
    • Lambda
  • Nested if-else example

jcg-assignment-ternary-operator.py

# assignment - python ternary operator

# example 1 - single line example
# syntax - value_if_true if condition else value_if_false

driving_age = 17

ans = "Eligible for driving" if driving_age >= 18 else "Not eligible for driving"

print("single line example= ", ans)

a, b = 20, 50

# example 2 - direct example

# example 2a - tuples
# syntax - (if_check_is_false, if_check_is_true)[condition]

print("\ntuple result= ", (a, b)[a > b])

# example 2b - dictionary

val1, val2 = 'a is smaller than b', 'b is greater than a'

# if a > b is True then the value of the true key will be returned.
# else if a > b is False then the value of the false key will be returned.
print("\ndictionary result= ", {True: val1, False: val2}[a > b])

# example 2c - lambda
# lambda assure that only one expression will be evaluated
print("\nlambda result= ", (lambda: val2, lambda: val1)[a > b]())

# example 3 - nested if-else example
if a > b:
    print("\na is greater than b")
else:
    print("\nb is greater than a")

3. Code run & demo

Run this python script and if everything goes well the output will be shown in the IDE console.

Fig. 1: Ternary Example Code output
Fig. 1: Ternary Example Code output

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

4. Summary

In this tutorial, we learned about the ternary operator in python and different implementations which we can use to play. You can download the source code of this tutorial from the Downloads section.

5. Download the Project

This was a tutorial on how to use the ternary operator in python programming.

Download
You can download the full source code of this example here: Ternary Operator in Python

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button