Open In App

Different Ways of Using Inline if (ternary operator) in Python

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Python offers a simple and concise way to handle conditional logic by using inline if, also known as the ternary operator or conditional expression. Instead of writing a full if-else block, we can evaluate conditions and assign values in a single line, making your code cleaner and easier to read for simple cases.

Syntax

<expression_if_true> if <condition> else <expression_if_false>

Parameters:

  • condition: A boolean expression to evaluate.
  • expression_if_true: Executed if the condition is True.
  • expression_if_false: Executed if the condition is False.

Return Type: Result of expression_if_true if the condition is true, otherwise, result of expression_if_false.

Let’s explore the different ways of using Inline if:

Basic Inline if with if-else

In this example, we are comparing and finding the minimum number by using the ternary operator.

Python
x = 10
s = "Even" if x % 2 == 0 else "Odd"
print(s)

Output
Even

Explanation: This is not a ternary expression, but rather a one-line if statement.

Basic Inline Using If -Else

In this example, if x is even, the variable message will be assigned the string “Even,” and if x is odd, it will be assigned the string “Odd.”

Python
x = 10
s1 = "Even" if x % 2 == 0 else "Odd"

x = 11
s2 = "Even" if x % 2 == 0 else "Odd"

print(s1)
print(s2)

Output
Even
Odd

Explanation: If x % 2 == 0 is true, “Even” is assigned to message; otherwise, “Odd” is assigned.

Using Inline If with nested

In this example, we use nested inline if statements to determine the relationship between the values of x and y.

Python
x = 10
y = 5

result = "x is even and y is odd" if x % 2 == 0 else "x is odd and y is even" if y % 2 == 0 else "both x and y are odd"
print(result)

Output
x is even and y is odd

Explanation: The code checks if x is even. If not, it checks y. If neither is true, it concludes both are odd.

Note: Nested inline if can reduce readability if overused.

Using Inline If in List Comprehensions

In this example, we use inline if within a list comprehension to include only even numbers in the list of squares.

Python
n = 10
squares = [x ** 2 for x in range(1, n + 1) if x % 2 == 0]
print(squares)

Output
[4, 16, 36, 64, 100]

Explanation: Only even numbers are considered, and their squares are added to the list.

Using Inline If with Function Calls

In this example, the operation variable is assigned the square function if n is even and the cube function if n is odd. The appropriate function is then called to calculate the result.

Python
def square(x):
    return x ** 2

def cube(x):
    return x ** 3

n = 5
operation = square if n % 2 == 0 else cube
result = operation(n)
print(result)

Output
125

Explanation: Since n is odd, the cube function is selected and applied to n.

Advantages and Disadvantages of Using Inline if

Advantages

  • Conciseness: Inline if statements make your code shorter and more readable by reducing the need for multiple lines of code for simple conditionals.
  • Clarity: They can improve code clarity when used appropriately, especially in situations where the condition and expressions are short and straightforward.
  • Readability: Inline if can make your code more readable by keeping the conditional logic close to where it’s used.

Disadvantages

  • Limited Complexity: They are not suitable for complex conditions or multiple statements within the condition or expressions, which can reduce code readability.
  • Overuse: Overusing inline if can make your code less readable, as complex expressions can become hard to understand in a single line.
  • Debugging: Debugging can be more challenging when using inline if, as you can’t set breakpoints within the conditional expression.


Next Article

Similar Reads