While doing programming, the coder needs to do concise and clean code. As a result, they prefer choosing the concise version of big statements. Inline if is a concise version of if…else statement can be written in just one line. It basically contains two statements and executes either of them based on the condition provided. So, let us see the various ways in which one can use inline if in python.
Ways to use Python inline if statement:
- Inline If Without Else
- With Else Statement
- Inline If Elif
Python Inline if Without else:
If <condition>: <statement>
- <condition>: the condition to be evaluated
- <statement>: executed if the condition evaluation is true
Example: Python Inline if without else
con = True if con:print('The condition is True')
Here, the con consists of the Boolean value True. As a result, the condition is satisfied, and the statement print (‘The condition is True’) is executed.
Python Inline if with else statement:
<statement1> if <condition> else <statement2>
- <statemetn1>: executed if the condition evaluation is true
- <condition>: the condition that will determine which statement to follow
- <statement2>: executed if the condition evaluation is false
Example: Python Inline if with else
color='blue' print ("The color is red" if color == 'red' else "The color is not red")
The value of color is ‘blue’. As a result, the condition doesn’t satisfy and so the statement in else part is executed.
Python Inline if with elif:
Although it can look a bit messy, writing an inline if- elif statement is possible and can be used as an expression wherever necessary.
Example: Python Inline if with elif
value = 10 print ("The value is less than 10" if value<10 else ("The value is more than 10" if value>10 else "The value is equal to 10"))
It will first compare and check the value of 10, whether or not it is lesser than 10. As it is false, it goes into the next statement and checks if the value is more than 10. And ultimately, since none of the condition satisfies, it executes the last else part and prints the statement The value is equal to 10
Advantages of Python Inline if:
By now, we have understood how to use an inline if statement in different forms. But if we want to implement it into our program, we should know about its advantage and its disadvantage.
Since it itself is an expression, it can be used inside other expressions as well. It can be used as an expression inside a list, lambda functions, etc.
L= [idx if idx<50 else idx-50 for i in range (1,100)] print(L)
In this example, we have used the inline if expression as an expression for the list. It checks that if the element is less than 50, it will add it to the list. Otherwise, it will subtract 50 from the number and then add it to the list.
Disadvantage of Python Inline if:
Being an expression, it definitely serves various purposes. However, it is also the cause of disadvantage for the inline if. Since it is an expression, one can’t use any statement inside it.
b=10 a= a-1 if b==10 else a = a+1
Syntax Error is that in the else part, a=a+1 is a statement, and since inline if it is an expression, one cannot write a statement inside it.
So, these are the various ways in which the inline if statement can be used. Since it is an expression, it comprises of both advantages and disadvantages. The usage of inline if should be done according to the program’s requirement to deliver the most optimized and accurate result.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.