Ternary Operator in Python:

Ternary operators are also known as conditional expressions. These operators evaluate base on a condition being TRUE or FALSE. It came into existence from Python version 2.5 for Data Science.

It makes programming more comfortable by allowing to test a condition in a single line rather than the multiline if-else and also makes the program compact.

  1. A simple method of using ternary operator:

code in python for data science

  1. Direct method using tuples, dictionary, and lambda:

data scince code

  1. Ternary operator is nest if-else:

python for data science

Important Points:

  • At the beginning, the given condition is (a<b), either a or b is return base on the boolean value return by the condition.
  • The order of arguments differs from other languages in the operator.
  • Amongst all Python operations, ternary operators have the lowest priority.

The method When Ternary Operator is Absent:

First, the interpreter checks the truth of the expression, if TRUE then on_true was evaluate, else on_false was evaluate.

Syntax:

Let’s look at an example:

If you want to learn more about operators in python for data science, you can check this and this.

Leave a Reply

Your email address will not be published. Required fields are marked *

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