The Python One-Line If Statement Shorthand

Introduction

The Python one-line if (a.k.a. single-line if) is a concise way for writing an if statement that executes only one line of code when the if condition is true.

The One-Line if Statement Syntax

Like the regular if statement in Python, the one-line if consists of the keyword if, followed by a condition and a colon :. Then, instead of going to the next line, as you would do with the regular if, you put a statement directly after the colon and a space on the same line.

Here is its syntax:

if condition: statement
  • condition: A boolean expression that determines whether to execute the statement or not.
  • statement: The code to execute if the condition is true. It can only be a single Python statement that takes up one line of code.

Example:

number_of_treats = 3
dog_name = input("Please enter the dog's name: ")
if dog_name == "Snoopy": number_of_treats += 2

print(f"{dog_name} will get {number_of_treats} treats.")

Output:

Please enter the dog's name: Snoopy
Snoopy will get 5 treats.

The code in the example above determines how many treats a dog will get. Any dog will get three, but only Snoopy will get two more. (Not fair!) The program works by prompting the user to input the dog’s name on line 2. Then, the one-line if statement on line 3 adds two more treats if the name is “Snoopy”.

Comparison With The Regular if

In contrast, the equivalent of a regular, traditional if statement is as follows:

if condition: 
    statement

The only change in the regular if above is that statement is indented and on the next line.

This is what the dog treat example looks like with a regular if on lines 3 and 4:

Example:

number_of_treats = 3
dog_name = input("Please enter the dog's name: ")
if dog_name == "Snoopy": 
    number_of_treats += 2

print(f"{dog_name} will get {number_of_treats} treats.")

When to Use the One-Line if

The one-line if is shorthand syntax that saves you the extra line and indent, and consequently, leads to cleaner and more concise code.

However, it’s more restrictive than the regular if and does not support the following:

  1. Multi-line statements
  2. An else clause
  3. elif clauses

It’s therefore useful for the simple instances in which you don’t need any of the above capabilities.

Note: Python has syntax called the ternary operator that looks like the following:

value1 if condition else value2

It is similar to a one-line if statement, and the two are often confused. The fundamental difference is that the ternary operator is an expression, which is a piece of syntax that always results in a value. It does not support statements, which do something but do not have to return a value.

In the dog treat example above, number_of_treats += 2 is a statement and therefore cannot be used within a ternary operator. The ternary operator will be covered by the next lesson.

Summary & Reference for the Python One-Line If Statement Shorthand

The one-line if statement in Python is a concise way to execute a single line of code when a specific condition is true. Similar to the regular if statement, it involves the if keyword, a condition, and a colon, but the statement is written directly on the same line. The syntax is as follows:

if condition: statement

The one-line if offers a cleaner and more concise alternative to the regular if statement, omitting the need for an additional line and indentation. However, it comes with the limitations of not supporting multi-line statements, an else clause, or elif clauses. In contrast to the traditional if statement, the one-liner is best suited for simple cases where the mentioned limitations are not a concern.