avatarBetter Everything

Summary

The web content introduces Python's conditional expressions, also known as ternary operators, to streamline if-else statements into single-line expressions.

Abstract

The article titled "Python one-liner if-else statements" delves into the use of conditional expressions as a concise alternative to traditional multi-line if-else statements in Python. It explains the structure of ternary operators, which consist of a value if the condition is true, the condition itself, and a value if the condition is false. The author demonstrates how to replace a standard if-else block with a one-liner ternary expression, improving code brevity. Furthermore, the article addresses the nesting of conditional expressions to handle scenarios that would typically require if-elif-else blocks, such as distinguishing between profit, break even, and loss. The author emphasizes the flexibility of conditional expressions by showing their incorporation into print statements and calculations. The article concludes by inviting readers to gain full access to similar content by joining Medium and supporting the author's work.

Opinions

  • The author endorses the use of conditional expressions for a cleaner and more efficient codebase.
  • They suggest that the traditional if-else statements are more verbose and less ideal compared to the compactness of ternary expressions.
  • The author implies that nested conditional expressions are a powerful tool for handling multiple conditions within a single line of code.
  • There is an opinion that readers should consider joining Medium as a way to support content creators and gain comprehensive access to informative articles.

Python one-liner if-else statements

In this article I want to introduce conditional expressions also known as ternary operators.

With a conditional expression you can set a value based on a condition in 1 line of code. Image by catalyststuff on Freepik

Conditional expression to replace if-else statement

The term ternary means consisting of 3 parts, the 3 parts of a ternary operator or conditional expression are:

  • value if condition is true
  • the condition
  • value if condition is false

Here is how you write it in Python: value_if_True if condition else value_if_False

Before we make a conditional expression, we will look at an example of an if-else statment to set the value of a variable based on a condition:

result = 200

if result>0:
    result_text = 'Profit'
else:
    result_text = 'Loss'

print(result_text)

This works fine and prints: Profit, but we can rewrite the middle part to make it 1 line of code instead of 4 by using a conditional expression.

The 3 parts of the conditonal expression are:

  • value if condition is true: 'Profit'
  • the condition: result>0
  • value if condition is false: 'Loss'

With the 3 parts combined you get the conditional expression:

result = 200

result_text = 'Profit' if result>0 else 'Loss'

print(result_text)

Conditional expression to replace if-elif-else statement

Maybe you spotted an error in our Profit or Loss expressions.

I wrote that if result is not higher than 0 result_text should be 'Loss'. But if the result is exactly 0 the result_text should actually be something like 'Break Even'.

We could implement that by specifying an extra condition. Here is an example of using the elif keyword in an if-else statement:

result = 0

if result>0:
    result_text = 'Profit'
elif result==0:
    result_text = 'Break Even'
else:
    result_text = 'Loss'

print(result_text)

This will correctly print: Break Even.

But now we are stuck with a long if-else statement. Or are we?

The answer is no, we can also write the above code to a one-liner. Let’s see it!

We start with the original conditional expression:

'Profit' if result>0 else 'Loss'

Then we will remove the value_if_False, which is 'Loss' in our case:

'Profit' if result>0 else

And we will place another conditional expression as the value_if_False of the above expression:

'Profit' if result>0 else 'Break Even' if result==0 else 'Loss'

Conditional expressions in Python statements

Besides it being way shorter, another advantage of conditional expressions is that you can also use them inside other Python statements.

For example, inside of a print statement:

print('Profit' if result>0 else 'Loss')

Or in a calculation:

buy_product_a = True
spendings = 300
total_spendings = spendings + 200 if buy_product_a else 0

Thank you for reading!

You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:

You might also like:

Python
Programming
Software Development
Tips And Tricks
Beginners Guide
Recommended from ReadMedium