Python can even process complex algorithms in a single line of code. Almost every programming language supports if-else statements such as Python, C++, etc. If-else statements also known as conditional statements are used in decision-making problems. They are like flow charts that control the flow of a program.

How To Use If-Else Statements?

The below-given code utilizes the one-line “if statement” that will check if the given number lies in the given range; if yes then it will print the given number:

if 60 in range(150): print("60")

The output of the above code is as follows:

One Line If-Else Statement Using Ternary Operator

With the use of a ternary operator, we are able to write if-else statements in one line. A ternary operator in Python is one that allows us to select one value among several choices based on multiple conditions.

Example 1: Traditional If Else Statement

Here is an illustration of an if-else statement without the use of a ternary operator.

if "Smith" in "My name is John Smith":
    x = "Smith"
else:
    x = "Joseph"
print(x)

The output of the above code is as follows:

Example 2: One line If-Then-Else Statement

We will now execute the previously mentioned example using a ternary operator:

x = "Smith" if "Smith" in "My name is John Smith" else "Joseph"
print(x)

It can be seen that in the above code:

  • We have assigned a value Smith to variable x.
  • If the condition “if Smith in My Name is John Smith” is true, it will print Smith.
  • Otherwise, if the condition is false, the name “Joseph” will be assigned to x.

The output is as follows:

As the given condition is true, it has printed Smith. Let’s see another example:

grade = 79
x = 'Oops! You failed' if grade < 50 else 'Congratulations!, you passed'
print(x)

It can be seen in the above code

  • We have assigned a value 79 to the variable grade.
  • If the condition “if grade <50” is true, it will print “Oops you failed”, and if the condition is false, it will print “Congratulations, you passed”.

As the given condition is false, it prints “Congratulations! You passed”

One-Line If-Elif Statement Using Ternary Operator

Additionally, we can use the ternary operator to write one-line if-elif statements as well.

age = 16
outcome = 'You are a Kid.' if age < 16 else 'Not sure...' if 16 <= age < 18 else 'Welcome'
print(outcome)

In the above code:

  • We have assigned a value of 16 to the variable age.
  • If the condition age<16 is true, the code will print “You are a kid”.
  • If the condition 16<= age <18  is true, the code will print “Not Sure”.

The output of the above code is as follows:

It can be seen that the second condition is true, hence it printed “Not sure”.

Advantages and Disadvantages of One-Line If-Then-Else Statements

We can create if-else statements in one line of code by utilizing the ternary operator. However, this approach has some advantages as well as disadvantages, as listed below:

Advantages:

  • Compared to standard if-else statements, one-line if-else statements are concise.
  • They make the code easier to read, especially when the conditions are simple.

Disadvantages:

  • One-line if-else statements are more complex and difficult to read.
  • For situations involving numerous conditions or nested if-else logic, they are not suitable.

Conclusion

Python if-else statements are conditional statements. By utilizing the ternary operator, We can create one-line if-else statements. A ternary operator in Python allows us to select one value among several choices based on multiple conditions. This article has described how to write if-then-else statements in one line in Python.