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.

    Table Of Contents - Python Ternary Operator

  1. Ternary operator in python
    1. Ternary operator syntax
    2. python ternary operator example
    3. python ternary assignment
  2. Nested ternary operator
  3. python ternary operator multiple conditions
  4. python ternary operator without else
  5. Conclusion

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.

Python ternary operator

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 condition ? expression1 : expression2 in other programming languages like C, C++, JavaScript, etc.

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.

Example 1

  • If-else
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")

Output:

Can vote

Example 2

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")

Output:

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.

Example 3

a, b = 10, 20

min = a if a < b else b
print(min)

Output:

10

Example 4

num = int(input("Enter a number: "))

# set 10 if num is greater than 10
num = 10 if num > 10 else num
print(num)

Output:

# 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.

Example 5

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)

Output:

20

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].

Example 6

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)

Output:

No
Yes

Python Ternary Operator Multiple Conditions

You can also use multiple conditions in a ternary operator by using logical operator keywords like and, or, not

Separate as many conditions with logical operators like and, or, not and wrap them in parenthesis.

Example 7

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)

Output:

No
Yes

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 and operator.

Here is an example of that:

a, b = 10, 20

result = (a < b) and "a is less than b"
print(result)

Output:

a is less than b

Code explanation

  1. The above code has the format of True/False and string
  2. If the first statement is false then the and operator will return False and the second statement will not be executed.
  3. 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.