In some cases, you are required to make a decision based on a certain condition. For all such decision-making applications, where you are required to take a certain action only when a particular condition holds true, we use if-else statements. This tutorial is about How to use the If Else statement in one line using Python.
If you want to learn more about Python programming, visit Python Tutorials.
One-Line If-Else Statements in Python
Writing a one-line if-else statement is possible by using three methods. In the first method, the multiple statements are replaced by a compact single statement. In the second method, conditional statements are used. In the third method, nested if-else is used.
What action is to be performed is defined by a certain piece of code that is written inside the body of if-else statements. You’re already familiar with the syntax of if-else statements. It is almost similar to the C++ language and is written as:
If (Condition):
Statements
Else:
Statements
If the condition holds true then the statements inside the body of the if-statement will execute otherwise the body of the else statement will execute. The statement can be a single instruction or multiple instructions. Now, we will learn how to write these statements in a one-line statement.
The syntax for a one-line statement is:
<instruction 1> If (condition) else <instruction 2>
Let’s understand the execution of these statements through different examples.
The simplest method to use if else statement in one line in Python
One-line If-Else statements are executed from left to right. The simplest approach for if-else statements in one line in Python is:
a = int(input("Enter any integer: "))
Print("Value entered by user is less than 50") if a < 50 else print ("Value entered is either greater or equal to 50")
Output:
Enter any integer: 95
Value entered is either greater or equal to 50
Now, in this example, the multiple statements are replaced by a compact single statement. Such types of conditional statements are known as ternary operators. Now, let’s see how nested if-else statements can be written in compact one-line expressions. Suppose you have to check whether a number entered by the user is positive, negative, or zero.
older approach to use conditional if else statement in single line python
This is an older approach to use conditional statements in Python.
a = int(input("Enter any integer: "))
if a < 50:
print("Value entered by user is less than 50")
else:
print("Value entered is either greater or equal to 50")
Output:
Enter any integer: 10
Value entered by user is less than 50
In this example, the first instruction takes an integer input from the user. The command input() is used to take input from the user. The command “int” converts the input entered by the user into an integer value. The input value is compared with 50. If it is less than 50, then the compiler executes the instruction inside the body of the if-statement.
nested if else in python
Nested if-else statements in one line can be executed as follows:
num = int("Enter integer: "))
print('Positive') if num > 0 else print ('Negative') if num < 0 else print ('zero')
Output:
Enter Integer: 7
Negative
In this example, the first condition is checked which is “num>0”. As the condition is false, the compiler moves on to the second statement which is “num<0”. The number entered by the user is -7 which is less than 0. Since the condition holds true, therefore the compiler prints ‘Negative’ on the output screen. Now, let’s suppose the user has entered 0 as an input. Then, in that case, the second statement would be false and the compiler would move to the third statement and the output would be “Zero”.
another example of if elif else in python
Let’s consider another example in which you have to take two integers as input from the user. Then ask the user to select the operation from the following +, -, x. Now, write a code using ternary if-else statements that perform the specified operation between the two integers.
a = int(input("Enter integer 1: "))
b = int(input("Enter integer 2: "))
oper = str(input("Select an operation (+, - , / , *)")
print("Sum: ", a + b) if (oper == '+') else print ("Difference: ", a - b) if (oper == ' - ') else print ("Product: ", a * b) if (oper == "*") else print (invalid entry")
Output:
Enter integer 1: 5
Enter integer 2: 6
Select an operation (+, - , / , *): -
Difference: -1
Just like python if else one-line statements, if you want to learn more about programming in Python Contact Us. Let us know about your feedback in the comments.