Short hand if else:We can say that the short hand if-else is a concise way to write if/else statements in Python, as it allows you to write an if/else statement on a single line of code or statement.
we can define short hand if else statement is
“An executable statement, written in so compact manner that it has only a single line of code is known as shorthand statement.”
Python shorthand If-else make conditional statements more readable and to the point.
Short hand if else in Python does not follow the same syntax as previous languages; however, the short hand technique does exist.
In Python, the short hand if else are implemented and the keywords if and else are used.
The advantage of using the shorthand if else in python programming is that i. the interpreter handles it in an efficient way.
ii. Less typing or code while using the shorthand.
For example, if we wanted to print given number is “positive” or "negative" if the number x is greater than 0 then number is "Positive", and “negative” if it is not, we could use this short hand(ternary) operator:
print("positive") if x > 0 else print("negative")
in the given expression if x is greater than 0
then print("positive ") shows output as "positive".
otherwise print("negative") shows the output "negative".
Short hand if else or conditional Statement in Python
Syntax : short hand if else statement in python
Given the Python syntax, the short hand if else or the ternary operator as follows:
# Python Ternary Operator
The syntax for using the ternary operator looks like this:
var1= exp1 if condition else exp2
Explanation:
The ternary operator is a condensed version of an if else statement. It is written as a single line of code and evaluates a condition to return one of two values.
Let us understand how does short hand if else statement works.
in the given above syntax if the condition is true then exp1 is get assigned to var1.
if condition is false the exp2 is assigned to var1.
Given fig. show how short-hand if else statement works in python.
Python starts script execution from top and goes to end.
vale 5 is assigned to the variable a and 3 is assigned to b.
i.e.
a=5
b=3
then we have statement.
print(a," is Maximum") if a>b else print(b,"is maximum")
here the condition if a>b is true i.e. 5>3 is true.
then the statement print(a," is Maximum") executes and which gives output--> 5 is maximum.
if condition is false then print(b,"is Maximum") executes.
Example 1: Python script to check The person eligible for voting or not.
age=int(input("Enter Your age"))
# Python Ternary Operator
can_vote = true if age >= 18 else false
if can_vote: #test-condition
print(" Wellcome You are Eligible for Voting!. and your age is = ",age)
else:
print(" OOps! You are not Eligible for Voting!. and your age is = ",age)
Output:
Enter Your age
18
Wellcome You are Eligible for Voting!. and your age is =18
Explanation:
After successful the program show message
" Enter Your age" and wait till we not enter age.
and the age entered is 18.
then statement can_vote = true if age >= 18 else false , which executes as successfully and assign true to can_vote because age=18.
then we have tested if can_vote: which true and thus shows output
"Wellcome You are Eligible for Voting!. and your age is =18"
Previous Topic:-->> Nested if else statement in Python || Next topic:-->>Python Conditional statements assignments