Python ternary operator

Everything Python ternary operator with example in 2023

In this blog, we will learn everything about the python ternery operator. We will understand the python ternary operator with various examples. so let’s get started.

What is python ternary operator?

python_ternary_operator

In Python, the ternary operator is a shorthand way to write a if-else statement. The ternary operator is added in python version 2.5. The ternary operator is mainly used for simple decision-making, when the if-else statement is too complex, it’s better to use the if-else statement.

The ternary operator makes the code more readable as it is a concise way of writing a simple if-else statement. A ternary operator can also be nested inside another ternary operator, but it can make the code hard to read.

The ternary operator is not unique to Python and can also be found in other programming languages.

Syntax of python ternary operator

The syntax of the ternary operator in Python is as follows:

value_if_true if condition else value_if_false

Here, condition is a boolean expression that is evaluated first. If condition is True, value_if_true is returned, otherwise value_if_false is returned.

For example:

A = 12
result = "A is greater than 15" if A > 15 else "A is less than or equal to 15"
print(result)

The ternary operator compares the value of A with the number 15, if it’s greater the operator returns “A is greater than 15” otherwise “A is less than or equal to 15”. In this case, 12 is not greater than 15 so the second option is chosen.

python ternary operator

Python ternary operator example

In this session, we will understand the python ternary operator with various examples.

Python ternary assignment

In Python, you can use the ternary operator to assign a value to a variable based on certain conditions. check the below example.

A = 5
B = 10
max_value = A if A > B else B
print(max_value)

This will assign a value of 10 to the variable max_value because the condition A > B is false and B is the value assigned to value_if_false.The output will be:

Python ternary assignment

Python ternary operator with tuple

You can use the ternary operator with a tuple to assign values based on certain conditions. Let’s take the below example:

A = 5
B = 10
max_value = (A, B) if A > B else (B, A)
print(max_value)

The ternary operator compares the value of A with the number B, if A is greater the operator returns tuple (A, B) otherwise (B, A). In this case, 5 is not greater than 10 so the second option (B, A) is chosen and assigned to the variable max_value and when you print the value of max_value it will output the tuple (10, 5).

Python ternary operator with tuple

Python ternary operator with the dictionary

you can also use the ternary operator to assign a dictionary to a variable based on a certain condition. Let’s take the below example

A = 5
B = 10
max_value = {'large': A, 'small': B} if A > B else {'large': B, 'small': A}
print(max_value)

In this case, 5 is not greater than 10 so the second option {'large': B, 'small': A} is chosen and assigned to the variable max_value and when you print the value of max_value it will output the dictionary {'large': 10, 'small': 5}.

Python ternary operator with the dictionary

Python ternary operator with the Lambda function

The ternary operator with lambda functions can be useful in situations where you want to keep the code concise and assign a function to a variable based on a certain condition.

The lambda function does not require a name, which makes it useful for simple, one-time-use functions. But for larger and more complex functions, it’s recommended to use regular Python functions. Let’s take the below example:

A = 5
B = 10
max_value = (lambda: A) if A > B else (lambda: B)
print(max_value())

In the above case, 5 is not greater than 10 so the second option (lambda: B) is chosen and assigned to the variable max_value and when you call the lambda function with max_value() it will output the value of B which is 10.

Python ternary operator with the Lambda function

Python ternary operator multiple conditions

You can also chain multiple conditions with the ternary operator to perform more complex operations. However, as the number of conditions increases and the conditions become more complex, the code can become harder to read and understand. To keep the code readable, it’s recommended to use if-else statements instead of chaining multiple ternary operators.

Let’s check the below example which finds the greatest digit from 3 numbers using the ternary operator.

A = 25
B = 15
C = 30
max_value = A if A > B and A > C else (B if B > C else C)
print(max_value)
Python ternary operator multiple conditions

Python ternary return

In Python, the ternary operator can be used to return a value based on a certain condition. Let’s take a simple example.

def get_max(A, B):
    return A if A > B else B

result = get_max(15, 10)
print(result)

When you call the function with get_max(15,10) the function will return 15 since 15 is greater than 10. And when you print the result the variable which is assigned the return value of the function will output 15

Python ternary return

Conclusion

I hope you found this tutorial on the python ternary operator helpful. Please let me know if you have any issues following it

More to explore

Python hashmap

Python NULL detailed tutorial

Python lowercase function

Python array length

Python xor function

Leave a Comment

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

Scroll to Top