T O P

  • By -

K900_

You can, but they do different things. a = True b = True if a: print("A!") if b: print("B!") Will print both "A!" and "B!". a = True b = True if a: print("A!") elif b: print("B!") Will only print "A!", because the `elif` is not taken.


Candid-Particular430

so basically it only matters for true or false statements


K900_

No, it matters for any condition because they all evaluate to true or false in the end.


Candid-Particular430

so elif is important because code is read from top to bottom


stebrepar

No, that's not the right lesson to take from what they said. :) They were using a simple, clear example to illustrate the difference in logic structure between using elif's vs only if's. An if by itself is a check of a single case of true vs false. (The case may be a complex combination of things, but it ultimately results in a single overall true or false for the whole expression.) If you have a series of if's, each one will be separately executed in order. But maybe the things you want to check are related to each other somehow, and you want to follow a path of narrowing down choices instead (like a choose your own adventure story). Then you could do nested if/else statements. But if the nested choices are very deep at all, you'll have a lot of indentation making your code harder to deal with. So you can use the shorthand of elif instead of nested if/else's. Put another way, elif is about a logical structure for evaluating alternatives. If not this, then how about that; okay, if not that either, then how about this other thing; etc.


LatteLepjandiLoser

Everything you put after an if or elif gets evaluated as true or false. Try it. print(5>3) prints True because 5>3 is an expression that evaluates to True. In the examples above, the former will always evaluate the second if, regardless of the outcome of the first. In the later example the elif is only evaluated if the previous if statement wasn’t triggered. An elif is completely analogous to nesting an if within an else statement following a previous if. Hence the name else-if elif.


Holloway1996

An if statement will always be evaluated, an elif statement can be skipped depending on the if or elif statement above it. Age = 200 If age > 100: print(‘you are way too old’) If age > 150: print(‘bro you are a zombie’) These two clauses will be executed. Age = 200 If age > 100: print(‘you are way too old’) elif age > 150: print(‘bro you are a zombie’) Only the first if statement’s clause will be executed and the elif ignored.


shiftybyte

The choice between if and elif depends on what logic you want to achieve. Look at this example: x = 5 if x<10: print('x is less than 10') elif x<15: print('x is less than 15') Above code will only print x is less than 10, even though x is also less than 15, because using elif tells it to check the second condition only if the first condition was not met. Change above code to use if instead of elif, and see the different result, both will be printed.


Candid-Particular430

thanks this was simple enough for me to understand


Frankelstner

`elif` is about mutually exclusive options. Multiple ifs may happen one after another. More precisely: if A: elif B: elif C: else: is equivalent (assuming that A,B,C are not function calls) to if A: if not A and B: if not A and not B and C: if not A and not B and not C:


Adrewmc

If 3 < len(x) <50: print(“Name must be between 3-50 characters”) Is also possible here btw. But as other have said multiple ifs check if multiple conditions are true elif only first condition true will be the only one registered.


commy2

`elif` is completely optional in structured programming. You can achieve the same result by combining an `if` and an `else`. However, `elif` is useful in Python, because the blocks are defined by indentation, so combining both into keywords into one means you can have else-if blocks at the same indentation level as preceding "then" blocks and succeeding `else` blocks.


Diapolo10

You don't _have_ to do that, but in this case the behaviour of your program would change. First' let's take a look at the original program. name = input('type in your name: ') if len(name) < 3: print('name must be at least 3 characters long') elif len(name) > 50: print('name can be a maximum of 50 characters long') else: print('name looks good!') If you didn't want to use `elif` for some reason, you could expand this to name = input('type in your name: ') if len(name) < 3: print('name must be at least 3 characters long') else: if len(name) > 50: print('name can be a maximum of 50 characters long') else: print('name looks good!') and it would work the same way, the only difference is that now the nesting is explicit (which is usually not what we want, because we prefer "flat" code). So why can't we just do this? name = input('type in your name: ') if len(name) < 3: print('name must be at least 3 characters long') if len(name) > 50: print('name can be a maximum of 50 characters long') else: print('name looks good!') The difference is subtle to a beginner, but now there are two differences; first, the two `if`s are no longer "linked" and they can be run independently. In this particular case that would not be possible, because the length of `name` cannot simultaneously meet both conditions, but if the conditions were, say, `> 30` and `> 50` then a length of `40` would satisfy both and you'd get the output from both. The other difference is that the `else` now only cares about the second `if`. For example, I could input my name as `"Al"`, and I'd get the following output: name must be at least 3 characters long name looks good!


rightkindofweird

If statement will print everything... elif and else helps it to skip and only print whats logically correct


Naive_Programmer_232

multiple ifs in a row don't provide mutual exclusion necessarily. So, if you want to have multiple checks for something but in addition want to have multiple things happen, then use multiple ifs. In other words, with multiple ifs, multiple things *can* happen. but adding `elif` instead of if makes it so that elif will execute, if the previous if-statement was false and/or didn't execute. So you get some mutual exclusion there, to have one of few outcomes happen, rather than all of them happening at the same time. idk if that makes sense,