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.
Frequently Asked Questions
How to write a one-line If-Then-Else statement in Python?
In Python, you can write a one-line If-Then-Else statement using a ternary operator. This allows you to select one value based on multiple conditions in a concise manner.
What is the purpose of If-Else statements in Python?
If-Else statements in Python, like in other programming languages, are used for decision-making. They control the flow of a program based on specified conditions.
Can Python process complex algorithms in a single line of code?
Yes, Python can handle complex algorithms in a single line of code. It offers concise syntax and powerful features to achieve this efficiency.
How can I check if a number lies within a specific range in Python?
You can check if a number lies within a specific range in Python using the 'if' statement with the 'in' operator. This allows you to easily determine if a number falls within a given range.
What is the output of the one-line If-Else statement using a ternary operator?
The output of a one-line If-Else statement using a ternary operator depends on the condition specified. It selects one value from multiple choices based on the condition.
How can I write an If-Else statement without using a ternary operator in Python?
To write an If-Else statement without a ternary operator in Python, you can use the traditional format with 'if' and 'else' blocks. This offers a more explicit way of handling conditional logic.
What is the advantage of using a ternary operator in Python?
A ternary operator in Python allows you to write concise and readable one-line If-Else statements. It provides a compact way to express conditional logic in your code.
How does Python handle decision-making with If-Else statements?
Python uses If-Else statements to control the flow of a program based on specified conditions. It provides a structured way to implement decision-making logic in your Python scripts.