The if and if-else statements are being widely used in Python. These statements are of great importance when writing Python code. These statements can be written in many ways. Sometimes a shorthand notation, also known as “inline is statements” or “ternary operators” is used to avoid unnecessary mess in the code. In this blog, we will be concentrating on the inline if statements for different conditions. Let’s get started with it.

How to Write/Use an Inline If Statement for Print in Python?

The inline if statements are used to write the if-else conditional statements in shorthand notation. The simple syntax for this notation is:

Return-this-Value-if-true if Condition else Return-this-Value-if-false
  • The first value is returned if the condition is true. The condition is written after this statement using the IF clause.
  • IF clause is followed by the else statement which is followed by the the return value when the condition given is false.

Let’s observe the syntax with the help of an example.

Example: Inline If Statement 

A simple example of an inline if statement is given below.

num = 12
print("greater than 10" if num > 10 else "smaller than 10")

In the above syntax:

  • We have declared the number as 12.
  • The inline statement is written in the print statement. 
  • The first statement i.e. “greater than 10” is returned/printed if the condition given i.e. “num > 10” is true.
  • Otherwise, the second statement is returned/printed.
  • We can see that in this case, the condition given is true i.e. 12>10 so the first statement will be printed.

The output of the above code looks like this:

We will now look at different scenarios in inline statements.

Inline If Statement with No Else Statement

If there is no else statement, we can simply write the inline if statements like this example:

status = True
if status: print("completed!")

Simply write the if condition. If the condition is true the declared statement will be returned. As in the above case, the statement is true so the status “completed!” will get printed. Like this:

Inline If Statement with Else Statement

The inline if statement with the else statement is also elaborated above. You can enhance the above example for this particular scenario.

Multiple Conditions in Python Inline If statement

To handle multiple conditions normally, we make use of the “elif” statements. The “elif” statement is the shorthand notation of if-else. We can also use the elif and inline format together. The elif statement in multi-line format is written as:

num = 25

if num < 10:
    str = 'number is less than 10'
elif num < 20:
    str = 'number is less than 20'
    str = 'number is greater than 20'


This format specifies multiple conditions in multiple lines. The specified number in the above code satisfies the third condition. The output of the above codes is:

 We can also write the same code in the inline if statement format like this:

num= 25
str = 'number is less than 10' if num < 10 else 'number is less than 20' if num < 20 else 'number is greater than 20'

This Inline if statement code is the same as the code we discussed earlier and will result in the same output as above. The output is:

So this was all about the simple Inline if statements for printing and returning values in Python. The advanced use cases for this statement are using the inline if statement in loops and using the conditions within conditions.

Limitations of Inline if Statement

The inline statement is used so that our code seems simple, concise, and more readable. But sometimes using this notation makes the code too complex and jumbled up so in this case it is better to revert back to the “multi-line if” structure so that it is easier to understand.


We use the inline statements to make our code more readable and easy to understand to be applied properly. This notation provides clarity but up to a certain limit. In this article, we have seen many scenarios of using Inline if-else statements along with their practical examples and their limitations.