Last Updated : 18 Dec, 2024
The ternary operator in Python allows us to perform conditional checks and assign values or perform operations on a single line. It is also known as a conditional expression because it evaluates a condition and returns one value if the condition is True and another if it is False.
Basic Example of Ternary OperatorThe simplest way to use a Python ternary operator is when we have a simple if else condition - either of the two conditions is True and the other is False.
Let’s start with a simple example to determine whether a number is even or odd:
Python
n = 5
res = "Even" if n % 2 == 0 else "Odd"
print(res)
The ternary operator can be used in various ways. Let us see a few different examples to use Ternary Operators in Python:
Ternary Operator in Nested If elseThe ternary operator can also be used in Python nested if-else statement. We can nest ternary operators to evaluate multiple conditions in a single line.
Syntax: value_if_true if condition else value_if_false
Example:
Python
n = -5
res = "Positive" if n > 0 else "Negative" if n < 0 else "Zero"
print(res)
Explanation:
The ternary operator can also be written by using Python tuples. The tuple indexing method is an alternative to the ternary operator.
Syntax: (condition_is_false, condition_is_true)[condition]
Example:
Python
n = 7
res = ("Odd", "Even")[n % 2 == 0]
print(res)
Explanation:
A dictionary can be used to map conditions to values, providing a way to use a ternary operator with more complex conditions.
Syntax: condition_dict = {True: value_if_true, False: value_if_false}
Example:
Python
a = 10
b = 20
max = {True: a, False: b}[a > b]
print(max)
Explanation: This uses a dictionary where the key is True or False based on the condition a > b. The corresponding value (a or b) is then selected.
Ternary Operator using Python LambdaLambdas can be used in conjunction with the ternary operator for inline conditional logic.
Syntax: lambda x: value_if_true if condition else value_if_false
Example:
Python
a = 10
b = 20
max = (lambda x, y: x if x > y else y)(a, b)
print(max)
Explanation: This defines an anonymous function (lambda) that takes two arguments and returns the larger one using the ternary operator. It is then called with a and b.
Ternary Operator with Print FunctionThe ternary operator can also be directly used with the Python print statement. Its syntax is a s follows:
Syntax: print(value_if_true if condition else value_if_false)
Example: In this example, we are finding the minimum number among two numbers using Python ternary operator with print statement.
Python
a = 10
b = 20
print("a is greater" if a > b else "b is greater")
Explanation: This checks if a is greater than b. If true, it prints "a is greater"; otherwise, it prints "b is greater".
Limitations of Python Ternary OperatorWhile the ternary operator is concise, it should be used with caution:
Ternary Operator in Python
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4