Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren0
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 Antworten
+ 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?