Python If Else in One Line
Last Updated :
18 Dec, 2024
In Python, if-else conditions allow us to control the flow of execution based on certain conditions. While traditional if-else statements are usually written across multiple lines, Python offers a more compact and elegant way to express these conditions on a single line.
Python if-else in One Line
In Python, a typical if-else statement is written in multiple lines. However, Python provides a shorthand way to write conditional expressions, which is known as the ternary operator.
Here’s a simple example to illustrate the concept:
Python
x = 10
y = 5
res = "x is greater" if x > y else "y is greater"
print(res)
In this example, the condition x > y is evaluated. Since it’s True, the string "x is greater" is assigned to the variable result. If the condition had been False, the string "y is greater" would have been assigned instead.
Syntax of one-liner if elif else Statement:
This syntax works by evaluating the condition first. If the condition is True, the expression before the else keyword is executed; if the condition is False, the expression after the else is executed.
value_if_true if condition else value_if_false
Example: Determine if a Number is Positive, Negative, or Zero
Python
n = -5
res = "Positive" if n > 0 else "Negative" if n < 0 else "Zero"
print(res)
Explanation:
- num > 0: The first condition checks if the number is greater than 0. If True, it returns "Positive".
- num < 0: If the first condition is False, it moves to the next condition. If True, it returns "Negative".
- else "Zero": If both conditions are False, it returns "Zero" as the default result.
One-Line If-Elif-Else in Python
Python does not directly support a true one-liner for if-elif-else statements like it does for a simple if-else. However, we can emulate this behavior using nested ternary operators.
Using Nested Ternary Operators
To write an if-elif-else condition in one line, we can nest ternary operators in the following format:
value_if_true1 if condition1 else value_if_true2 if condition2 else value_if_false
Example:
Python
x = 15
res = "Greater than 20" if x > 20 else "Greater than 10" if x > 10 else "10 or less"
print(res)
Explanation:
- x > 20 is checked first. If True, it returns "Greater than 20".
- If not, x > 10 is checked. If True, it returns "Greater than 10".
- If none of the conditions are True, it falls to the final else value: "10 or less".
Similar Reads
Python For Else The for else loop in Python is a unique feature that adds flexibility to control flow. It allows you to distinguish between loops that complete naturally and those interrupted by a break. By understanding and utilizing this construct, you can write more expressive and intentional Python code.Underst
2 min read
Python Else Loop Else with loop is used with both while and for loop. The else block is executed at the end of loop means when the given loop condition is false then the else block is executed. So let's see the example of while loop and for loop with else below. Else with While loop Consider the below example. Pytho
3 min read
Indentation in Python In Python, indentation is used to define blocks of code. It tells the Python interpreter that a group of statements belongs to a specific block. All statements with the same level of indentation are considered part of the same block. Indentation is achieved using whitespace (spaces or tabs) at the b
2 min read
Multiline Comments in Python A multiline comment in Python is a comment that spans multiple lines, used to provide detailed explanations, disable large sections of code, or improve code readability. Python does not have a dedicated syntax for multiline comments, but developers typically use one of the following approaches:It he
4 min read
Code Golfing in Python Code Golf in Python refers to attempting to solve a problem using the least amount of characters possible. Like in Golf, the low score wins, the fewest amount of characters "wins". Python is a fantastic language for code golfing due to backward compatibility, quirks, it being a high-level language,
8 min read