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???

3rd Dec 2017, 11:56 AM
Jinsu
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
3rd Dec 2017, 12:00 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
3rd Dec 2017, 12:16 PM
Amir Galanty
Amir Galanty - avatar
0
@Amir Galanty Smoliar Thanks for all those explanations, but I donā€™t get it. Can you explain it more easily please?
3rd Dec 2017, 12:34 PM
Jinsu