Table of Contents
The if-else statement is one of the common statements of many programming languages like Python, C/C++, and Java. It is a conditional statement and is used to perform some action based on a condition.
With the if-else statement, we run a block of code based on the evaluation of a provided condition. If the condition is True, the code from the if
block is executed, otherwise the code from the else
block is executed.
We can also have an if-else ladder where we can use multiple elif
statements to evaluate many conditions.
In some other programming languages, there also exists a ternary operator which can be thought of as a compact form of the if-else statements in one line. Python does not support this operator. However, we can use the if-else in one line in Python.
The syntax for if-else in one line in Python
To use the if-else in one line in Python, we can use the expression: a if condition else b
. In this expression, a
is evaluated if the condition
is True. If the condition
is false, then b
is evaluated.
Let us use this as an example.
1 2 3 4 5 |
a = 5 x = 10 if a==5 else 15 print("X is 15") if x != 10 else print("X is 10") |
Output:
In the above example,
- The value of variable
x
is assigned using the single line if-else statement. - Since the condition is True, the value of
x
is assigned as 10. - We again use the if-else to print the value of
x
. - Since the condition is False, the expression of the
else
statement is evaluated.
This is the traditional way to use the if-else in one line in Python. There are other ways also to emulate this. However, it is not recommended to use these because there is less consistency and usually both the expressions are evaluated first.
Further reading:
Using tuples to emulate if-else in one line in Python
A tuple can store multiple items in Python. We can use tuple-indexing to emulate if-else in one line in Python. The expression for this is given as: (b,a)[condition]
We will use a tuple with two values. The value a
is evaluated if the condition
is True, and b
for when it returns False.
See the code below.
1 2 3 4 5 |
a = 5 x = (15,10)[a==5] print(x) |
Output:
The above code returns the value of x
as 10 because the given condition is True.
Using dictionaries to emulate if-else in one line in Python
A dictionary contains key-value pairs. It can be used to perform if-else in one line in Python. This method is similar to the previous one. The expression used in this method: {True:a, False:b}[condition]
.
We will have a dictionary containing two keys, True
and False
. These keys will have some value associated with them. A condition will be evaluated in the []
and based on the returned Boolean value the corresponding element will be accessed.
We implement this in the code below.
1 2 3 4 5 |
a = 5 x = {False:15,True:10}[a==5] print(x) |
Output:
The condition in the above example returns True. Hence, the value associated with the key True
is retrieved from the dictionary.
Using and
and or
operator to emulate if-else in one line in Python
The and
and or
are logical operators in Python. The and
operator returns True when both values are True and the latter returns True if at least one value is True.
We can use them to emulate if-else in one line in Python using the following expression. condition and a or b
. In this expression, a
is evaluated when the condition
is True and b
when it is False. However, this is not a consistent method.
The inconsistency arises when the value of a
is False. During that case, b
will be evaluated every time. Still, it can be used in simple cases.
For example,
1 2 3 4 5 |
a = 5 x = a==5 and 10 or 15 print(x) |
Output:
Conclusion
This tutorial demonstrated the basics of the if-else statement and how to use if-else in one line in Python. We also demonstrated several methods to emulate the behavior of if-else in one line. These methods included the use of tuples, dictionaries, and logical operators.
Although these methods work similarly to if-else in one line in Python, their downside is that both the conditions are evaluated first before going to the condition. This is avoided in the if-else statements as one block is ignored based on the condition. Therefore, one should focus on the main syntax of if-else statement to use it in one line.
That’s all about If-else in one line in Python.