What's the difference between if and elif statements in PYTHON?

Home » Programming & Design » What's the difference between if and elif statements in PYTHON?
Programming & Design No Comments

I've noticed in a couple of practice code from a site where I'm learning Python that sometimes the code will only have if statements, and no elif.
What about only if's:
if x == 1: print('one')
if x == 2: print('two')
else: print('other')

Other answer:

PENYOU:
Regarding the update: "What about it?" is a good question, and one that you can get the answer to by trying it in a program. Do wonder about these things, but don't just wonder. Get typing and run code to SEE what happens. Put that code in a loop that varies x over a set of values that includes the interesting ones.

for x in range(5):
…. if x == 1:
…. …. print("one")
…. if x == 2:
…. …. print("zwei")
…. else:
…. …. print("other:", x) # added x to the output here

I've used …. tokens so you can see the indentation. Type spaces or tabs when you type it in.

Then change the second if to an elif and see how that changes the output. (Look carefully…there's one line you won't see now.)

What's going on is that the elif clause is shorthand for an else clause that has an if statement in it. That is:

if condtion1:
…. statements1
elif condition 2:
…. statements2

is identical to:

if condition1:
…. statements1
else:
…. if condition2:
…. …. statements2

Either way, if condition1 evaluated as True then the test for condition2 will not happen.

In a longer example:

if condition1:
…. statements1
elif condition2:
…. statements2
elif condition3:
…. statements5
elif condition4:
…. statements5
elif condition5:
…. statements5
else:
…. none_of_the_above_statements

What happens here is that the conditions are tested in order until one evaluates as True. If that happens, the statements associated with that if/elif test are executed and any remaining conditions (as well as the else clause) are ignored. The conditions aren't even tested.

The final else clause runs if (and only if) all of the if/elif tests failed.

All of that is part of a single compound statement. Technically, "elif" does not begin a statement. Like "else", it's a continuation of a compound statement previously begun by an "if" at the same indentation level.

If you used if instead of elif each time, then every condition would be tested every time, and the final else would only be associated with the final if, and would run whenever that final if condition evaluated as False. Each "if" line begins a new, independent compound statement. Assuming that none of the indented statements does a break, continue, return and no exceptions occur, then every one of those if statements runs in order.

AnnaJ:
.

In simple terms, control flow is the path of actions/events that happen in your program. In the simplest case, the path is linear (one statement is executed after another in the order that they appear in the source code). The code on the left creates the control flow you see on the right.

if…elif…else statements introduce branches in the control flow.

Let a, b, and c be conditions (Boolean expressions that can be either True or False, such as x < 3). Then an if…else makes a branch:

The program will only do this if a is True, and do that instead if a is False.

Each elif lets you introduce another branch:

The important thing to note here is that, in order to get to do that, not only b has to be True, but a has to be False. And that’s the difference between an if…elif…else and a series of ifs:

if a:
do this
elif b:
do that
elif c:
do something else
else:
print "none of the above"
is equivalent to (exactly the same as) this:

if a:
do this
if (not a) and b:
do that
if (not a) and (not b) and c:
do something else
if (not a) and (not b) and (not c):
print "none of the above"
As you can see, the elif…else notation allows you to write the same logic (the same control flow) without repeating the a condition over and over again.

Richard J:
elif is short for else if, like the last guy already told you.

When you have an if statement, the program evaluates if it is true and executes its code if it is.

An elif statement is only evaluated and, if true, executed PROVIDED the previous if and elif statements are not true, otherwise it's ignored.

Example:
grade = 95
if grade >= 90:
…print "A"
elif grade >= 80:
…print "B"
elif grade >= 70:
…print "C"

The output will be
A
Because the following elif statements aren't even evaluated. Now go and change all the "elif" to "if" and the output will be
A
B
C

Because 95 >= 90 so it prints
A
95 >= 80 so it prints
B
95 >= 70 so it prints
C

So use elif when you want it to only choose one option, use if for everything if it can select multiple options.

Daniel B:
elif is short for Else If and is used when you want to do more then one comparision in an if statement. For example:

if x == 1:
print('one')
elif x == 2:
print('two')
else:
print('other')

LEAVE A COMMENT