Python Ternary Operator - If Else One Line
In this tutorial, we will learn about the Python ternary operator. Moreover, we will look at its syntax, usage, and examples.
Ternary operator in python
- Ternary operator syntax
- python ternary operator example
- python ternary assignment
- Nested ternary operator
- python ternary operator multiple conditions
- python ternary operator without else
Table Of Contents - Python Ternary Operator
Ternary Operator In Python
The ternary operator in Python is a conditional operator that has three operands. The ternary operator is used to evaluate a condition and return either of the two values depending on the condition.
The ternary operator is just like a python if else statement but more clean, concise, and easy to understand.
Ternary operator syntax
The syntax of Python ternary operator is as follows:
<expression1> if <condition> else <expression2>
The 'condition' is the condition that is to be evaluated. The 'expression1' and 'expression2' are the expressions that are evaluated based on the condition.
The first condition is evaluated. If the condition is true, then the 'expression1' is evaluated and the value is returned. Otherwise, the 'expression2' is evaluated and the value is returned.
Note: Python ternary operator is the same as
Python Ternary Operator Example
The following example shows the usage of the ternary operator in python and also compares its code with the if-else statement.
- Ternary Operator
age = 25 # using ternary operator print("Can vote" if age >= 18 else "Can not vote")
age = 25 # using if-else if age >= 18: print("Can vote") else: print("Can not vote")
Here is another simple example of a ternary operator.
a, b = 10, 20 print("a is greater than b" if a > b else "a is less than b")
a is less than b
The above example compares the value of a and b, if a is greater than b then it prints "a is greater than b" otherwise it prints "a is less than b".
Python Ternary Assignment
The ternary operator is mostly used in assigning values to variables. When you have to decide different values of a variable based on the condition, then you can use the ternary operator.
Using a ternary operator for assigning values also makes the code more readable and concise.
a, b = 10, 20 min = a if a < b else b print(min)
num = int(input("Enter a number: ")) # set 10 if num is greater than 10 num = 10 if num > 10 else num print(num)
# let input be 22 # output will be 10 # let input be 5 # output will be 5
Nested Ternary Operator
The ternary operator can also be nested. This means you can have a ternary operator inside another ternary operator.
A nested ternary operator is used to evaluate results based on multiple conditions.
a, b = 10, 20 num = a if a > b else b if a < b else a # same as -> a if (a > b) else (b if a < b else a) print(num)
Code explanation: Return the value of a if a is greater than b [
a if a > b else (b if a < b else a)] , else return the value of b if a is less than b, else return the value of a [
b if a < b else a].
Here is an example of 3 nested ternary operators.
# divisible by 2, 3 and 5 # use ternary operator a, b = 12, 30 divA = "No" if (a % 2 != 0) else ("No" if (a % 3 != 0) else ("No" if (a % 5 != 0) else "Yes")) divB = "No" if (b % 2 != 0) else ("No" if (b % 3 != 0) else ("No" if (b % 5 != 0) else "Yes")) print(divA) print(divB)
Python Ternary Operator Multiple Conditions
You can also use multiple conditions in a ternary operator by using logical operator keywords like
Separate as many conditions with logical operators like
not and wrap them in parenthesis.
a, b = 12, 30 # divisible by 2, 3 and 5 divA = "Yes" if (a % 2 == 0 and a % 3 == 0 and a % 5 == 0) else "No" divB = "Yes" if (b % 2 == 0 and b % 3 == 0 and b % 5 == 0) else "No" print(divA) print(divB)
The above code checks if a and b are divisible by 2, 3, and 5 using multiple conditions. If all conditions are true then it prints "Yes" otherwise it prints "No".
Python Ternary Operator Without Else
Python does not allow using ternary operator without else.
But you can create similar functionality by using the
Here is an example of that:
a, b = 10, 20 result = (a < b) and "a is less than b" print(result)
a is less than b
- The above code has the format of
True/False and string
- If the first statement is false then the and operator will return False and the second statement will not be executed.
- If the first statement is true then the and operator will return second statement.
Python Ternary Operator Conclusion
Ternary operator in python is an alternate way of writing if-else which is concise and simple.
It first evaluates the given condition, then executes expression based on the condition then returns the value.
The ternary operator is not always useful. It is helpful when you have only one statement to execute after the condition is evaluated.