Python If Else Shorthand
Python If Else Shorthand

Python If Else Shorthand

Python is known for its easy-to-understand syntax and precise code. Be it loops or conditional statements like if-else, we can always cut down on the lines of code in Python. Python If-else shorthand is one such method to make those conditional statements more readable and to the point. We can also say that the Python if-else shorthand is like a Python ternary operator.

In this article, we will learn about these concepts in detail.

Python If-Else Shorthand

The If-Else statements help in decision-making in all programming languages and Python is no different. But before we learn about the Python if-else shorthand, let us first see how if-else statements work in Python.

Different Types of If-Else Statements in Python

We don’t always necessarily need an else statement if it is not required. In such cases, we can simply use the if statement to check a certain condition.

Let us consider the following example.

x = 15 
if x < 10:
    print("The if statement executes")
print("Thank you!")

Output

Thank you!

In the above example, the value of x is not less than 10 as given in the if statement. Hence, the code inside the if block won’t be executed. But the last statement executes irrespective of the result of the if statement because it is outside the if block.

Now let us add the else block to this code to make it more functional. You can see that the control of the program goes to the else block when the if condition is not met.

In other words, we say that if the condition is true, then the control goes to the if block and if it is false, the control goes to the else block.

x = 15 
if x < 10:
    print("The if statement executes")
else:
    print("The value of x is not what you want and thus, you are in the else block.")
print("Thank you!")

Output

The value of x is not what you want and thus, you are in the else block.
Thank you!

Now, consider the following scenario. Suppose that you want to check if a number is less than 20 and further, find out if it is even or not.

This is where the nested if statement comes in handy. Here, we first have an if block that checks the first condition, and later, inside the same block, we use another if statement to check the second condition.

x = 15
if x < 20:
    print("The number is less than 20.")
    if x%2 == 0:
        print("The number is even as well.")

Output

The number is less than 20.

Instead of using nested if blocks, We can also go ahead and add else statements in the above code as shown below.

x = 15
if x < 20:
    print("The number is less than 20.")
    if x%2 == 0:
        print("The number is even as well.")
    else:
        print("The number is odd.")
else:
    print("The number is greater than 20.")

Output

The number is less than 20.
The number is odd.

If you are dealing with a lot of conditions, then you can use the if-elif-else ladder. Here is an example of the same.

x = 7
if x == 5:
    print("It's the middle number 5")
elif x < 5:
    print("The number is closer to 0")
elif x > 5:
    print("The number is closer to 10")
else: 
    print("The number is invalid.")

Output

The number is closer to 10

So far so good, but don’t you think that the code might get a bit untidy if we have to check a good number of conditions? And that’s where the if-else shorthand comes into the picture.

Python Shorthand Example for If-Else Statements

Look at this piece of code where there is only one if statement and one else statement.

x = 2
y = 1342

if y%x == 0:
    print("Divisible")
else:
    print("Non-divisible")

Output

Divisible

Now let us use the Python shorthand to write these statements in a single line!

x = 2
y = 1342

print("Divisible") if y%x == 0 else print("Non-Divisible")

Output

Divisible

Here is another example. Say you want to do something like the code given below.

x, y = 3, 5
ans = x > y? 6 : 10

This is similar to a ternary statement but in Python, we can do this with the help of if-else shorthand as shown below.

x, y = 3, 5

ans = 6 if x > y else 10

print(ans)

Output:

10 

You can observe that since the value of x is not greater than the value of y, the value that gets assigned to the variable ans is 10.

So to break down what we read so far, we can say that a Python if-else shorthand has the following syntax.

do x if condition true else do y 

In the above syntax, the condition is checked, and depending on the result, either one of x or y will get executed. A True condition takes the control of the program to the x part whereas a False condition takes it to the y part of the if-else shorthand.

Following is an another example where we check various conditions in a single if-else shorthand statement in Python.

Roy = 10
Maddy = 20
print("Roy is younger.") if Roy<Maddy else print("Both are the same age.") if Roy==Maddy else print("Maddy is younger.")

Output

Roy is younger.

This is essentially how the Python if-else shorthand works.

But that’s not it! There is one more thing in Python that you can use as ternary operators.

Read on!

Use Tuples as a Ternary Operator in Python

Yes, you heard it right. We can make use of a Python tuple to make our code work like a single-line ternary operator. Here is an example of the same.

child = 'present'

send_confirmation = (False, True)[child == 'absent']
print(send_confirmation)

Output:

False

Although this method works fine, it is not advisable to use it because it might be confusing for some people to put the values false and true in the right places inside the tuple.

That’s all for this article!

To Sum It Up

In this article, we learned about Python if else shorthand along with the basics of conditional statements in Python. We also saw how the if-else shorthand works like a ternary operator along with the use of a tuple for the same.

To learn more about python programming, you can read this article on sys.argv in Python. You might also like this article on if-else vs try except in Python.

Hope this was helpful. Happy learning!

Donate to Avid Python

Dear reader,
If you found this article helpful and informative, I would greatly appreciate your support in keeping this blog running by making a donation. Your contributions help us continue creating valuable content for you and others who come across my blog. 
No matter how big or small, every donation is a way of saying "thank you" and shows that you value the time and effort we put into writing these articles. If you feel that our article has provided value to you, We would be grateful for any amount you choose to donate. 
Thank you for your support! 
Best regards, 
Aditya
Founder

If you want to Pay Using UPI, you can also scan the following QR code.

Payment QR Code
Payment QR Code

Leave a Reply