Hi. What is the diferents , from this :
if arguments :
elfi arguments:
elfi arguments:
elfi arguments:
else:
arguments
And this:
if arguments :
if arguments :
if arguments :
I mean, can we write only “if”, withoout elif and else. and why?
Hi. What is the diferents , from this :
if arguments :
elfi arguments:
elfi arguments:
elfi arguments:
else:
arguments
And this:
if arguments :
if arguments :
if arguments :
I mean, can we write only “if”, withoout elif and else. and why?
The elif statement is an abbreviation of “else if” so this…
if a:
foo
elif b:
bar
…is equivalent to this…
if a:
foo
else:
if b:
bar
As you can see, under some circumstances these may be more
efficient, because the second condition is only evaluated if the
first condition is false. That is to say, if a is true, b won’t be
checked and that can save a bit of processing time (or a lot,
depending on what’s involved in checking b). If you’re optimizing
conditional ladders like these, it helps to put the most likely
conditions (or the cheapest ones) earliest in order so that the less
likely conditions (or perhaps the more expensive conditions) can be
skipped more frequently.
Thank you very much sir
Some examples of difference:
Only if-s (without else) may result in no-match and therefore no if-branches will be executed. On the other hand if there is else branch it will be executed when there are no if-matches
In case of if-elif-else only one brach will be executed (first match or else clause) and even if there are other matches they will not be executed . If there are only if-s they will be checked one after another. If there are several matches every one of them will be executed.
The elif statement is an abbreviation of “else if” so this…
if a:
foo
elif b:
bar
…is equivalent to this…
if a:
foo
else:
if b:
bar
As you can see, under some circumstances these may be more
efficient, because the second condition is only evaluated if the
first condition is false. That is to say, if a is true, b won’t be
checked and that can save a bit of processing time (or a lot,
depending on what’s involved in checking b). If you’re optimizing
conditional ladders like these, it helps to put the most likely
conditions (or the cheapest ones) earliest in order so that the less
likely conditions (or perhaps the more expensive conditions) can be
skipped more frequently.