Javatpoint Logo
Javatpoint Logo

Ternary Operator in Python

It should be a priority for any developer who working on the Python programming language to write short, efficient, clean, and readable lines of code. In order to make things easier, Python provides the Ternary operators which offer a shorter and convenient method of writing conditional statements.

The Ternary operator in Python allows us to evaluate if a condition is True or False. This operator occupies only one line of code, which implies that it is more concise than a complete if-else statement.

Conditional statements, like if-else statements, helps us to regulate the program's flow. The snippet of code within the conditional statements only executes when a specific condition (or set of conditions) is satisfied.

In programming languages like Python, the if-else statement is the most common method in order to write conditional statements. However, the introduction of the ternary operator in Python offers a way to test for a condition on one single line.

In the following tutorial, we will explore more about Ternary Operator, how it works, and work on some examples in the Python programming language.

But before we get to that, let us discuss the basics of conditional statements.

Understanding Conditional Statements in Python

While writing Python code, we may sometimes want to execute a block or line of code when a specific condition is satisfied. In order for us to perform such tasks and functionalities, the conditional statements become handy.

The if-else statement in Python is utilized in order to check if a condition is met.

Let us consider an example. Suppose that we are creating an application that checks if a customer is eligible for a 15% discount at a movie theater. If the customer is aged 60 or above, they should be provided a discount; otherwise, no discount should be provided. We could use an if-else statement in order to create such a program.

Example:

Output:

First Number: 10
Second Number: 20
Larger Number: 20

Explanation:

In the above snippet of code, we have defined some variables as first_num, second_num, and max, specifying them with values like 10, 20, and None. We have then used the if-else statement to check which number is more significant. At last, we have printed the values for the users. As a result, the program evaluated the statements and returned the appropriate output.

However, as we can observe, the if-else statement consumes multiple lines of code. Thus, in order to reduce these lines of code, Python developers adopt a more concise way to write the conditional statement while evaluating a few conditions only, that is, the Ternary Operator in Python.

Let us explore the concept of Ternary Operator in the Python programming language.

Understanding the Ternary Operator in Python

The Ternary Operator is a type of condition expression in the Python programming language that allows the developers to evaluate statements. The Ternary Operators execute an action depending on if that statement either True or False. Thus, these operators are more concise than a traditional if-else statement.

Let us consider the following syntax for the Ternary statement in Python.

Syntax:

The name of the Python Ternary conditional operator is derived from the fact that it accepts three parameters: true_condition, expression, and false_condition.

The Ternary Operators are generally utilized in order to determine the variable's value. The variable takes on the value of "true_condition" if the statement results in True or "false_condition" if the statement results in False.

We can think of the ternary operators as a list comprehension used in the Python programming language in order to filter out a list. Or we can also think of it as a lambda function that is used to define a function.

Both lambda functions and list comprehensions are more effective methods in order to perform any action (such as defining a function and list filtration, respectively). Similarly, a ternary operator turns out to be a more efficient and effective method to write an if-else statement.

However, like ternary operators, we should use them in order to improve the code's readability and should not go overboard with the ternary operators; otherwise, the code may become much harder to read.

Now let us consider an example based on the Ternary Operator in Python

Suppose that we want to include a discount option at an event booking application for customers of age 60 or older. If a customer is younger than age 60, they are not eligible for a discount. We could check whether a customer is entitled to receive a discount using the snippet of code shown below:

Example:

Output:

Not Eligible for Discount

Explanation:

In the above snippet of code, we have defined a variable storing the value of the customer's age. We have then used the Ternary operator in order to check if the provided customer's age lies under the discount eligibility criteria or not and printed the result for the users. Since the value was 40, which is below 60, thus the program returned the else statement for the users.

If we try comparing the above example to the previous one, we can observe that this method utilizes significantly lesser lines of code.

Let us consider another example for better understanding. Suppose that we need to set a particular rate of discount relying on if the customer lies within an eligibility criterion. By default, our event booking application is providing all customers a 10% discount; however, customers of age 60 or older are eligible for a 20% discount.

A snippet of code is given below, which checks if a customer is eligible for the regular discount or the senior discount.

Example:

Output:

Customer's Age:  40
Discount provided: 10

Explanation:

In the above snippet of code, we have defined a variable storing the value of the customer's age. We have then used the Ternary operator in order to check if the provided customer's age lies under the senior discount eligibility criteria or not and printed the result for the users. Since the value was 40, which is below 60, thus the program returned 10 as the eligible discount statement for the users.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA