Why do we need elif operation(python)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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