In this article, you will learn about the Python ternary operator with the help of examples. Ternary operators in Python avoid writing multiple lines of code. Using the ternary operator you can check the condition in only one line of code.
Python ternary operator is also called condition operator, This is because it can evaluate a condition as an expression with the condition being True or False.
Headings of Contents
Python Ternary Operator
Ternary operator in Python is a type of condition operator that evaluates an expression based on the condition being True or False. Python ternary operator is more concise than traditional Python if-else statement. The ternary operator is able to check conditions in only one line of code.
Advantages of Ternary Operator
The advantages of using ternary operator in Python.
- If allowed writing an if-else statement in a single line.
- Increase code reusability.
- Avoid writing multiple lines of if code.
- Easy to remember.
- Increase code readability.
Syntax
The syntax of Python ternary operator is.
[on_true] if [expression] else [on_false]
Example: Python Ternary Operator
Without Ternary:
a,b = 12, 20
if a < b:
print(f"{a} is less than {b}")
else:
print(f"{b} is less than {a}")
# Output
# 12 is less than 20
With Ternary:
a,b = 120, 20
result = print() if a < b else print(f"{b} is less than {a}")
# Output
# 12 is less than 20
Python Ternary operator with tuple:
You can use ternary operator with Python tuple.
Syntax
(if_test_false, if_test_true)[test]
Example: Python ternary operator with tuple
a,b = 120, 200
print((f"{b} is less than {a}", f"{a} is less than {b}")[ a < b ])
# 120 is less than 200
Python ternary operator with Dictionary
Ternary operator can also be used with Python dictionary.
Example
a,b = 120, 200
print({True:f"{b} is less than {a}", False:f"{a} is less than {b}"}[ a > b ])
# 120 is less than 200
Ternary operator with Python lambda
You can also use Python lambda function with ternary operator and this is most efficient way rather than above.
Example
a,b = 120, 200
print((lambda: a, lambda: b)[ a < b ]())
Ternary operator can be written as nested if else
In this example, we will see all about ternary operator as nested if else statement.
Example
a, b = 100, 50
print("Both a and b are equal" if a == b else "a is less than b" if a < b else "a is greater than b")
# a is greater than b
Above example can be written without ternary operator.
Example
a, b = 100, 50
if a != b:
if a > b:
print("a is greater than b")
else:
print("b is greater than a")
else:
print("a and b both are equal")
Conclusion
So, in this article, we will learn all about Python ternary operator with different different types with the help of the examples.
Ternary operator in Python is the best way to use if-else statement instead of tradition if-else statement. I strongly recommend you, if you need to use an if-else statement in your Python code or project, Then always use Python ternary operator because this increases the code readability and clarity.
I hope this article will help you. If you like this article, please share and keep visiting for further Python tutorials.
Python operators
- Python Operators
- Python Arithmetic Operators
- Python Comparison Operators
- Python Assignment Operators
- Python Bitwise Operators
Thanks for reading