Python supports various conditional statements, such as if-else, equal to, greater than, etc. The Python if-else conditional statements are used to handle the multiple conditions in a program.
Python “if-else” can be written in one line using the conditional expression or ternary operator. By using the Python one-line “if-else” we can replace multiple lines of code with a single line and increase the quality of the code.
In this blog, we will cover the Python one-line if-else statement using multiple examples. The below-listed topics will be elaborated on in this Post:
- How to Create One Line If Else in Python?
How to Create One Line If Else in Python?
To create one line if else statement the following syntax of conditional expression/ternary operator is used in Python:
expression_value if condition else other_expression_value
In the above syntax, the first expression value will be returned to output when the “if” condition is satisfied, and when the “if” condition does not satisfy then the else block will execute its expression value.
Let’s see various examples of Python one-line if else statements for a profound understanding:
Example 1: Creating One Line If Statement
The following code is used to create one line if statement in Python:
number = 18 if number>10: print(number)
The “if” statement will show the given number value if the condition “number>10” becomes “True”.
The if statement successfully returned the expression value.
Example 2: Creating One Line If-Else Statement
The above example can’t handle the false conditions because it didn’t have the else block. So, the given below code is used to create the one-line if-else statement:
x = 18 y = 20 output = 'x is Greater!' if x > y else 'y is Greater!' print(output)
- When the “if” condition becomes “True” the first expression “x is Greater!” will be returned on screen.
- When the “if” condition becomes “False” the expression “y is Greater!” will be returned on screen.
The above snippet successfully executes the Python one-line if-else statement.
Example 3: Creating One Line If-Elif-Else Statement
To create the one-line “if-elif-else” statement the below code is used in Python:
X = 100 Y = 400 Z = 920 condtion_1 = X>Y and X>Z condtion_2 = Y>X and Y>Z output = 'X is Greater' if condtion_1 else 'Y is Greater' if condtion_2 else 'Z is Greater' print(output)
- The variables named ‘X’, ‘Y’ and ‘Z’ are initialized.
- The multiple conditions are initialized and assigned to a variable named “condtion_1‘ and “condtion_2”.
- When the “if condition” is satisfied, the expression “X is Greater” will be returned, and when it is not, the expression “Y is Greater” will be returned.
- When both “if” and “elif” conditions are not satisfied then the “else” statement will execute the expression “Z is Greater”.
The Python one-line “if-elif-else” has been successfully executed.
Note: Using a one-line if-else statement with multiple complex conditions will increase the chances of errors. So, it is recommended to use fewer complex conditions or use only those conditions that we are completely familiar with.
The Python one-line “if”, “if-else” and “if-elif-else” statements are created using the ternary operator or conditional expression. The Python one-line if-else statement is utilized to substitute numerous lines of code with a single line. This tutorial presents an in-depth understanding of how to create and use one-line if-else statements in Python.