In this article, we will learn how to use if-else in one line in python.
Other programming languages like C++ and Java have ternary operators, which are useful to make decision making in a single line. Python does not have a ternary operator. But in python, we can use the if-else in a single line, and it will give the same effect as the ternary operator.
So, let’s see how to use if-else in one line,
if..else in a single line in python like a ternary operator
In python, we can convert the if…else statement to a one-line conditional expression. For this, we need to write it in a specific format, checkout its syntax,
Syntax of if…else in one line or ternary operator
value_1 if condition else value_2
When the condition evaluates to True, then the result of this one-liner if..else expression will be value_1. Whereas, if the condition evaluates to False, then the result of this one-liner expression will be value_2. Let’s see some examples of it.
Example of if…else in one line
'High' if x > 10 else 'Low'
In this one-liner expression, we are using an if…else statement in a single line.
- If the value of x is greater than 10, then the expression will return ‘High’.
- If the value of x is less than 10, then the expression will return ‘Low’.
We can assign the value returned by the expression to another variable.
Now let’s see how to use this one liner if else expression,
x = 18 result = 'High' if x > 10 else 'Low' print(result)
Here the value of x was 18, which is greater than 10. So one-liner if-else expression evaluated to ‘High’ and got assigned to variable result.
x = 5 result = 'High' if x > 10 else 'Low' print(result)
Here the value of x was 5, which is less than 10. So one-liner if-else expression evaluated to ‘Low’ and got assigned to variable result.
Use if…else statement in one line carefully
While using the if-else statement in one line, we should prefer using parenthesis to avoid confusion. Checkout following example,
x = 20 result = 10 + 10 if x > 100 else 0 print(result)
In this example, the conditional expression x>100 evaluated to False, and the value of the else portion, i.e., 0 was returned.
But what is the value of if portion in the above example? Is it 10 or 10 + 10 ?
The value of if-portion is 10+10, i.e., 20.
But it is a little confusing here because some might think that the if..else expression is 10 if x > 100 else 0 and then 10 to the value returned by expression. By this logic if x is less than 100, then the value of this expression should be 10. But that is wrong in this case.
So, to avoid such kind of confusion, we should proper brackets/parenthesis while using if…else in one line, like this,
x = 20 result = 10 + (10 if x > 100 else 0) print(result)
Now in this one-liner if-else statement, the value of the if-portion is 10, and the value in else-portion is 0.
Although python does not have a ternary operator, but still using if-else in one line, we can have an effect of a ternary operator in python.
Pandas Tutorials -Learn Data Analysis with Python
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.