Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use if vs elif in python

If I have a function with multiple conditional statements where every branch gets executed returns from the function. Should I use multiple if statements, or if/elif/else? For example, say I have a function:

def example(x):     if x > 0:         return 'positive'     if x < 0:         return 'negative'     return 'zero' 

Is it better to write:

def example(x):     if x > 0:         return 'positive'     elif x < 0:         return 'negative'     else:         return 'zero' 

Both have the same outcome, but is one more efficient or considered more idiomatic than the other?

Edit:

A couple of people have said that in the first example both if statements are always evaluated, which doesn't seem to be the case to me

for example if I run the code:

l = [1,2,3]  def test(a):     if a > 0:         return a     if a > 2:         l.append(4)  test(5) 

l will still equal [1,2,3]

like image 211
Sean Geoffrey Pietz Avatar asked Apr 01 '14 10:04

Sean Geoffrey Pietz


People also ask

Why is it better to use Elif instead of if?

The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is False , it checks the condition of the next elif block and so on. If all the conditions are False , the body of else is executed.

What is the difference between if else and if Elif in Python?

The first form if-if-if test all conditions, whereas the second if-elif-else tests only as many as needed: if it finds one condition that is True, it stops and doesn't evaluate the rest. In other words: if-elif-else is used when the conditions are mutually exclusive. Hope this answers your question!!!

When should I use Elif?

Use the elif condition is used to include multiple conditional expressions after the if condition or between the if and else conditions. The elif block is executed if the specified condition evaluates to True . In the above example, the elif conditions are applied after the if condition.

When should Elif be used in Python?

The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE. Similar to the else, the elif statement is optional.


2 Answers

I'll expand out my comment to an answer.

In the case that all cases return, these are indeed equivalent. What becomes important in choosing between them is then what is more readable.

Your latter example uses the elif structure to explicitly state that the cases are mutually exclusive, rather than relying on the fact they are implicitly from the returns. This makes that information more obvious, and therefore the code easier to read, and less prone to errors.

Say, for example, someone decides there is another case:

def example(x):     if x > 0:         return 'positive'     if x == -15:         print("special case!")     if x < 0:         return 'negative'     return 'zero' 

Suddenly, there is a potential bug if the user intended that case to be mutually exclusive (obviously, this doesn't make much sense given the example, but potentially could in a more realistic case). This ambiguity is removed if elifs are used and the behaviour is made visible to the person adding code at the level they are likely to be looking at when they add it.

If I were to come across your first code example, I would probably assume that the choice to use ifs rather than elifs implied the cases were not mutually exclusive, and so things like changing the value of x might be used to change which ifs execute (obviously in this case the intention is obvious and mutually exclusive, but again, we are talking about less obvious cases - and consistency is good, so even in a simple example when it is obvious, it's best to stick to one way).

like image 65
Gareth Latty Avatar answered Sep 28 '22 04:09

Gareth Latty


Check this out to understand the difference:

>>> a = 2 >>> if a > 1: a = a+1 ... >>> if a > 2: a = a+1 ... >>> a 4 

versus

>>> a = 2 >>> if a > 1: a = a+1 ... elif a > 2: a = a+1 ... >>> a 3 

The first case is equivalent to two distinct if's with empty else statements (or imagine else: pass); in the second case elif is part of the first if statement.

like image 38
marco Avatar answered Sep 28 '22 03:09

marco