0
Why do we need elif operation(python)?
Why do we need elif operation if we can just simply do it with if? For example, if 1 == 3: print(āhiā) elif 1 == 1: print(āhelloā) if 1 == 3: print(āhiā) if 1 == 1: print(āhelloā) What is the difference???
3 Answers
+ 11
if (true):
#something
if (true):
#something
#both statements will be executed
if (true):
#something
elif (true):
#something
#only the first statement will be executed
#elif evaluates only if the previous if/elif evaluated to false.
https://stackoverflow.com/questions/25703446/whats-the-main-difference-between-if-and-else-if
+ 2
Sometimes, the result depand on all the conditions
if g % 7 == 0:
if f % 3 == 0:
do x
elif f % 5:
do x2
elif g % 5 == 0:
if f % 3 == 0:
do y
elif f % 5 == 0:
do y2
etc.
p.s.
+1 for @Hatsy Rei, if we fond that the first condition is enough, why keep asking?
It's not efficient.
0
@Amir Galanty Smoliar
Thanks for all those explanations, but I donāt get it. Can you explain it more easily please?