Simplifying conditional statements with the ternary operator in Python

·

3 min read

In Python, the ternary operator is a concise way to write conditional statements. It consists of a simplified, one-line version of the if...else statement to test a condition.

The ternary operator has the following syntax:

option1 if condition else option2

When a program encounters this statement, it first checks the condition specified after the if keyword. As for a normal if...else statement, it evaluates the condition:

If condition evaluates to True:

  • option1 will be executed

  • option2 will be skipped

If condition evaluates to False:

  • option1 will be skipped

  • option2 will be executed

Here is an example of a ternary operator:

age = 24
print("You are an adult") if age > 18 else print("You are not an adult")
# Ouptut => You are an adult

In this example, we defined the age variable and stored the integer 24 in it. Advancing in the program, we want it to check whether the value held inside the age variable is greater than 18. Since the value held inside the age variable is 24, option1 will be executed and option2 will be skipped.

Using the ternary operator can make your code more concise and easier to read, especially when you have a simple if...else statement. However, keep in mind that it is frowned upon by some Python developers due to its order of arguments being different from other languages and because it could make your code harder to read or maintain in larger codebases.

Oftentimes, we want to store the return value of the ternary operator in a variable. Our program will therefore look something like this:

x = 3
y = 5
greater = x if x > y else y
print(greater) # Output => 5

In the code above, we defined two variables x and y and respectively assigned the values 3 and 5 to them. The ternary operator then checks if x is greater than y. In this case, x is not greater than y, therefore the value of y will be assigned to the variable greater.

Note that you cannot use statements or assignments within a ternary expression. For example, an if...else statement like the one below cannot be written with the ternary operator.

user_name = "John"
if user_name == "John":
    greeting = "Welcome back " + user_name
else:
    greeting = "Unknown username, please retry"
print(greeting)
# Output => Welcome back John

In this example, the value of the greeting variable depends on the user_name variable. Since we are assigning the value to the greeting variable inside the if...else statement, we cannot turn it into a ternary operator.

Conclusion

In conclusion, the ternary operator in Python is a useful tool for simplifying conditional statements and making your code more concise. It allows you to write if...else statements in a more compact and readable way and can be used for variable assignments and other operations based on a condition.

While it is important to keep in mind the potential readability and maintainability issues that can arise with overusing the ternary operator, it is a great addition to any Python developer's toolkit. By carefully considering your use case and utilizing the ternary operator appropriately, you can write cleaner, more efficient code.