can someone explain the difference in use of ‘elif’ over ‘if’ in this problem?
def applicant_selector(gpa, ps_score, ec_count):
if gpa >= 3.0 and ps_score >= 90 and ec_count >= 3:
return "This applicant should be accepted."
elif gpa >= 3.0 and ps_score >= 90 and ec_count < 3:
return "This applicant should be given an in-person interview."
else:
return "This applicant should be rejected."
def applicant_selector(gpa, ps_score, ec_count):
if (gpa >= 3.0 and ps_score >= 90 and ec_count >= 3):
return "This applicant should be accepted."
if (gpa >= 3.0 and ps_score >= 90 and ec_count < 3):
return "This applicant should be given an in-person interview."
else:
return "This applicant should be rejected."
the top is the solution code and only varies from mine in the use of elif, however, it gave my code the green check mark.
Is there a way to make this code more succinct? It feels a little clunky as I am having to repeat variables that remain the same in various cases/
if - elif - else sets up a chain whereby the second condition will only be evaluated and its block executed if the first if condition does not return True, and else only if both do not return True.
if - if - if sets up a chain in which eachif statement will be evaluated ans its block executed if True, regardless of any prior results.
if - if - else likewise sets up a chain in which bothif conditions will be evaluated and their blocks executed if True, but else will be evaluated only if the must recent if (i.e., the second one) is False.
Of course, depending on the specific conditions, as in the case you cite, the results might be the same for every possible set of inputs.
So, consider: In the example you cite, if condition 1 returns True, why should your code even bother to evaluate the second? Using elif eliminates that need. It doesn’t seem like much here, but what if the if and elif conditions involved a massive data search or a particularly complex calculation? You could save some significant computational power by a judicious choice of if - elif vs if - if.
How to simplify? Leverage the facts that (1) the first two lines differ only in the third condition, (2) that the third condition in line 1 can only be True or False, and (3) that executing returnhalts processing of the function.
# Your own version is equivalent to this:
if condition_1 and condition_2 and condition_3:
return result_1
if condition_1 and condition_2 and not condition_3:
return result_2
else:
return result_3
# ... so how about:
if condition_1 and condition_2:
if condition_3:
return result_1
else:
return result_2
return result_3
# ... or make use of Python's "ternary operator" feature:
if condition_1 and condition_2:
return result_1 if condition_3 else result_2
return result_3
(And, you could double-up the ternary operator to get a one-liner, but that,IMHO, would be obfuscating, rather than simplifying.)
Once you cover dictionaries, you’ll be able to eliminate many if - elif chains altogether.
Thank you for this explanation! One follow up on your code simplification:
# ... so how about:
if condition_1 and condition_2:
if condition_3:
return result_1
else:
return result_2
return result_3
wouldn’t you want an “else result_3” instead of “return result_3”? So it appears to me that the return statement is paired with the initial “if” condition_1 and condition_2 thus result_3 will be given in the event that conditions 1 & 2 are met which is not the desired outcome. Maybe I am just confused on the use of “return” to halt a function? Anything that clarifies would me much appreciated!
No. If conditions 1 & 2 are met, then one of either (1) the subsequent if or (2) its following else block will be executed, and the result of both of these is return something, which means that result_1 or result_2 is returned, and the function halts (for that is what return does.). The line, return result_3 is (in that case) never reached, as @mtf states.
Say that condition_2 is False. Then the initial if statement returns False, and the subsequent if - else block is not entered, but is completely bypassed and control passes to the subsequent line, return result_3:
Play around with the conditions in line 1 to see how it works.
When return is reached, the function
Evaluates the expression to the right of return to obtain a value
Assigns that value to the calling statement
Halts the function, which disappears (along with any of its local variables) from the call stack (Python’s “short term memory” of active processes) until called again.
@patrickd314 I wanted to ask/clarify a part of your answer with an example, I’m referring to your second point:
if - if - if sets up a chain in which each if statement will be evaluated
and its block executed if True, regardless of any prior results.
I believe that statement is false because in the case that the IF STATEMENTS return something, the first IF STATEMENT if TRUE will execute that block and all subsequent IF STATEMENTS will be skipped.
Example:
def applicant_selector_two(gpa, ps_score, ec_count):
if gpa>=3.0 and ps_score>=90 and ec_count>=5:
return "You're definitely in."
if gpa>=3.0 and ps_score>=90 and not ec_count>=3:
return "You're probably in"
else:
return "You're out."
print(applicant_selector_two(4,100,5))
print(applicant_selector_two(4,100,4))
The console in this example prints:
You’re definitely in.
You’re out.
I assume the original post was referring to repeating if statements in general rather than this specific case where you happen to be in a function. The rest of the description does state that after return is met then the function finishes processing. So you are correct that if you return inside the function then the rest of that function block is not evaluated.