Google News
logo
Python Program to Short hand if ... else
In the following example of Python program that demonstrates how to use the shorthand `if...else` statement :
Program :
# Using the shorthand if...else statement
x = 5

print("x is greater than 0" if x > 0 else "x is less than or equal to 0")
Output :
x is greater than 0
In this program, we use the shorthand `if...else` statement to check the value of the variable `x`. If `x` is greater than `0`, the program will print the message "x is greater than 0". If `x` is not greater than `0`, the program will print the message "x is less than or equal to 0".

The shorthand `if...else` statement is a more compact way of writing the standard `if...else` statement. It allows you to perform a quick check and take one of two actions based on the result. You can use this statement to write more concise code.