Shortening Python Conditionals

In Python if else can be shorted in many ways. But before that let’s see the most common form of conditional statements in python.

if boolean-expression:
    statement
else:
   another statement

Actually, it is really common to see conditional statements in Python this way. If you consider the readability of the code then I will surely recommend this.

However, I do not think that it is really a necessity in Toph cause you can find your name in the problems if you solve it probably in the shortest way possible (or at least shorter than others). Cause the earliest, the fastest and the lightest title most generally goes to whoever solves the problem first.

Now then how can we do things to achieve that title with python in a pythonic way and probably while reducing the characters in the code?

If you are common to C or C++ then you are probably expecting something like this.

variable = (boolean-expression) ? value if true :  value if false;

Of course, something like this doesn’t exist in python but ways to get the same result in a more pythonic way does exist. Let’s try to solve a problem and try to find out what they are actually. To serve as a perfect example I have chosen a simple leap year problem. Not an actual leap year problem but a leap year with given conditions.

In this problem, a leap year is simply a leap year if just the year is equally divided by 4. If we use the traditional way then the solution would be something like this.

def isLeapYear(year):
    if year % 4 == 0:
        return true
    else:
        return false

or something like this actually works also,

def isLeapYear(year):
    return year % 4 == 0

but let’s drop this for now.

So, the solve simply becomes,

def isLeapYear(year):
    if year % 4 == 0:
        return true
    else:
        return false

Y = int(input())

if isLeapYear(Y):
    print("Yes")
else:
    print("No")

But that is probably not the thing that we want.

let the function actually hold isLeapYear(year) actually hold the answer string rather than using it inside a conditional statement. Since we are changing the code, we will call the function leapYearVerifyString(Year). The number of characters used to write it will be far less this way.

def leapYearVerifyString(year):
    string = ""
    if year % 4 == 0:
        string = "Yes"
    else:
        string = "No"
    return string

Y = int(input())

print( leapYearVerifyString( Y ) )

This way it is much better. Some people also do, this skipping a part of the conditional statement some time.

def leapYearVerifyString(year):
        string = "No"
        if year % 4 == 0:
            string = "Yes"
        return string

This way putting the else part is unnecessary.

But still, the code feels too much heavy for a puny problem like this.

in python, you can use this form of conditional statements to assign values to variables also.

variable = value-if-true if boolean-expression else value-otherwise

so we can simply assign the string like this. (finally!)

def leapYearVerifyString(year):
        string = "Yes" if year % 4 == 0 else "No"
        return string

Some pro also does this

string = "No" if year % 4 else "Yes"

but let’s drop this for now.

Or, you can simply write.

def leapYearVerifyString(year):
    return "Yes" if year % 4 == 0 else "No"

Finally, a single line of code is all which was required!!! You are probably wondering how it works?
its simple. if year % 4 == 0 is true then the value “Yes” is return or else the value “No” is the string which is returned. So, in case of a leap year, it is,

def leapYearVerifyString(year):
        return "Yes" # value "Yes" is returned cause year is equally divided by 4

otherwise, it is,

def leapYearVerifyString(year):
            return "No" # value "No" is returned cause year is NOT equally divided by 4

So, the code we have to actually write down is this,

year = int(input())
print("Yes" if year % 4 == 0 else "No")

What? No one said that you cannot use this inside another function such as print(). The value is returned in the same way as mentioned above. :upside_down_face:

Now if you want to shorten it further than you can use the structures mentioned down also

variable = boolean-expression and vaule-if-true or value-otherwise

or,

variable = ( value-if-false, value-if-true)[ boolean-expression ]

So,

string = "Yes" if year % 4 == 0 else "No" # or,
string =  year % 4 == 0 and "Yes" or "No" # or,
string = ( "No", "Yes" ) [year % 4 == 0]

all produce the same outcome.

1 Like

Note:

variable = ( value-if-false, value-if-true)[ boolean-expression ]

This method is not actually a standard but merely indexing a tuple.

The value of a true boolean expression is 1 and it is 0 when the expression is false.

So, when the expression is true, you are accessing the element which is in index 1 and the value in index 0 otherwise.

2 Likes